Django REST框架创建一个简单的Api实例讲解


Posted in Python onNovember 05, 2019

Create a Simple API Using Django REST Framework in Python

WHAT IS AN API

API stands for application programming interface. API basically helps one web application to communicate with another application.

Let's assume you are developing an android application which has feature to detect the name of a famous person in an image.

Introduce to achieve this you have 2 options:

option 1:

Option 1 is to collect the images of all the famous personalities around the world, build a machine learning/ deep learning or whatever model it is and use it in your application.

option 2:

Just use someone elses model using api to add this feature in your application.

Large companies like Google, they have their own personalities. So if we use their Api, we would not know what logic/code whey have writting inside and how they have trained the model. You will only be given an api(or an url). It works like a black box where you send your request(in our case its the image), and you get the response(which is the name of the person in that image)

Here is an example:

Django REST框架创建一个简单的Api实例讲解

PREREQUISITES

conda install jango
conda install -c conda-forge djangorestframework

Step 1

Create the django project, open the command prompt therre and enter the following command:

django-admin startproject SampleProject

Step 2

Navigate the project folder and create a web app using the command line.

python manage.py startapp MyApp

Step 3

open the setting.py and add the below lines into of code in the INSTALLED_APPS section:

'rest_framework',
'MyApp'

Step 4

Open the views.py file inside MyApp folder and add the below lines of code:

from django.shortcuts import render
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from django.http import JsonResponse
from django.core import serializers
from django.conf import settings
import json
# Create your views here.
@api_view(["POST"])
def IdealWeight(heightdata):
 try:
  height=json.loads(heightdata.body)
  weight=str(height*10)
  return JsonResponse("Ideal weight should be:"+weight+" kg",safe=False)
 except ValueError as e:
  return Response(e.args[0],status.HTTP_400_BAD_REQUEST)

Step 5

Open urls.py file and add the below lines of code:

from django.conf.urls import url
from django.contrib import admin
from MyApp import views
urlpatterns = [
 url(r'^admin/', admin.site.urls),
 url(r'^idealweight/',views.IdealWeight)
]

Step 6

We can start the api with below commands in command prompt:

python manage.py runserver

Finally open the url:

http://127.0.0.1:8000/idealweight/

Django REST框架创建一个简单的Api实例讲解

References:

Create a Simple API Using Django REST Framework in Python

以上就是本次介绍的关于Django REST框架创建一个简单的Api实例讲解内容,感谢大家的学习和对三水点靠木的支持。

Python 相关文章推荐
linux环境下安装pyramid和新建项目的步骤
Nov 27 Python
Python编程实现微信企业号文本消息推送功能示例
Aug 21 Python
PyQt5每天必学之拖放事件
Aug 27 Python
Python3内置模块random随机方法小结
Jul 13 Python
Python使用matplotlib绘制Logistic曲线操作示例
Nov 28 Python
Python内置函数property()如何使用
Sep 01 Python
python中如何打包用户自定义模块
Sep 23 Python
Python中random模块常用方法的使用教程
Oct 04 Python
Python实现手势识别
Oct 21 Python
python 爬取豆瓣网页的示例
Apr 13 Python
教你利用Selenium+python自动化来解决pip使用异常
May 20 Python
Python使用华为API为图像设置多个锚点标签
Apr 12 Python
python中for循环变量作用域及用法详解
Nov 05 #Python
Python对Excel按列值筛选并拆分表格到多个文件的代码
Nov 05 #Python
pytorch torch.expand和torch.repeat的区别详解
Nov 05 #Python
Python socket模块ftp传输文件过程解析
Nov 05 #Python
python3.6、opencv安装环境搭建过程(图文教程)
Nov 05 #Python
Python socket模块方法实现详解
Nov 05 #Python
基于python3 的百度图片下载器的实现代码
Nov 05 #Python
You might like
很实用的一个完整email发送程序
2006/10/09 PHP
探讨:如何使用PhpDocumentor生成文档
2013/06/25 PHP
php遍历文件夹下的所有文件和子文件夹示例
2014/03/20 PHP
PHP预定义超全局数组变量小结
2018/08/20 PHP
thinkphp整合系列之极验滑动验证码geetest功能
2019/06/18 PHP
Javascript与flash交互通信基础教程
2008/08/07 Javascript
jQuery UI Dialog控件中的表单无法正常提交的解决方法
2010/12/19 Javascript
jQuery获取节点和子节点文本的方法
2014/07/22 Javascript
JS对大量数据进行多重过滤的方法
2016/11/04 Javascript
JavaScript 事件流、事件处理程序及事件对象总结
2017/04/01 Javascript
vue如何集成raphael.js中国地图的方法示例
2017/08/15 Javascript
详解如何在webpack中做预渲染降低首屏空白时间
2018/08/22 Javascript
Angular angular-file-upload文件上传的示例代码
2018/08/23 Javascript
解决echarts数据二次渲染不成功的问题
2020/07/20 Javascript
详解vue3中组件的非兼容变更
2021/03/03 Vue.js
[03:40]DOTA2亚洲邀请赛小组赛第二日 赛事回顾
2015/01/31 DOTA
python基础入门学习笔记(Python环境搭建)
2016/01/13 Python
Sanic框架Cookies操作示例
2018/07/17 Python
python数组循环处理方法
2019/08/26 Python
CSS3混合模式mix-blend-mode/background-blend-mode简介
2018/03/15 HTML / CSS
HTML5+lufylegend实现游戏中的卷轴
2016/02/29 HTML / CSS
函授本科自我鉴定
2013/11/03 职场文书
18岁生日感言
2014/01/12 职场文书
护士检查书
2014/01/17 职场文书
医药类个人求职的自我评价
2014/02/12 职场文书
爱国主义演讲稿
2014/05/07 职场文书
2015年六一儿童节演讲稿
2015/03/19 职场文书
2015年质量月活动总结报告
2015/03/27 职场文书
2015年销售工作总结范文
2015/03/30 职场文书
结婚通知短信怎么写
2015/04/17 职场文书
2015小学师德工作总结
2015/07/21 职场文书
中秋节祝酒词
2015/08/12 职场文书
关于拾金不昧的感谢信(五篇)
2019/10/18 职场文书
浅谈Python项目的服务器部署
2021/04/25 Python
Python requests用法和django后台处理详解
2022/03/19 Python
Java后端 Dubbo retries 超时重试机制的解决方案
2022/04/14 Java/Android