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 相关文章推荐
python常用web框架简单性能测试结果分享(包含django、flask、bottle、tornado)
Aug 25 Python
python调用fortran模块
Apr 08 Python
django 在原有表格添加或删除字段的实例
May 27 Python
python检测主机的连通性并记录到文件的实例
Jun 21 Python
详解python的四种内置数据结构
Mar 19 Python
Python3安装Pillow与PIL的方法
Apr 03 Python
python交互模式下输入换行/输入多行命令的方法
Jul 02 Python
详解用python生成随机数的几种方法
Aug 04 Python
python matplotlib imshow热图坐标替换/映射实例
Mar 14 Python
windows上彻底删除jupyter notebook的实现
Apr 13 Python
关于jupyter打开之后不能直接跳转到浏览器的解决方式
Apr 13 Python
Python 解析简单的XML数据
Jul 24 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
php htmlspecialchars加强版
2010/02/16 PHP
来自phpguru得Php Cache类源码
2010/04/15 PHP
PHP表单验证的3个函数ISSET()、empty()、is_numeric()的使用方法
2011/08/22 PHP
php微信开发之关注事件
2018/06/14 PHP
php app支付宝回调(异步通知)详解
2018/07/25 PHP
深入理解 PHP7 中全新的 zval 容器和引用计数机制
2018/10/15 PHP
javascript new一个对象的实质
2010/01/07 Javascript
jqGrid读取选择的多行的某个属性代码
2014/05/18 Javascript
JQuery弹出炫丽对话框的同时让背景变灰色
2014/05/22 Javascript
js中的事件捕捉模型与冒泡模型实例分析
2015/01/10 Javascript
使用jquery提交form表单并自定义action的实现代码
2016/05/25 Javascript
Google 地图控件集详解及实例代码
2016/08/06 Javascript
用jQuery的AJax实现异步访问、异步加载
2016/11/02 Javascript
基于Vue开发数字输入框组件
2017/12/19 Javascript
vue+axios 前端实现的常用拦截的代码示例
2018/08/23 Javascript
Vue组件Draggable实现拖拽功能
2018/12/01 Javascript
JavaScript中如何对多维数组(矩阵)去重的实现
2019/12/04 Javascript
简单上手Python中装饰器的使用
2015/07/12 Python
python获取程序执行文件路径的方法(推荐)
2018/04/26 Python
python-itchat 获取微信群用户信息的实例
2019/02/21 Python
Python 20行简单实现有道在线翻译的详解
2019/05/15 Python
Python 转换RGB颜色值的示例代码
2019/10/13 Python
python 实现二维列表转置
2019/12/02 Python
详解Python高阶函数
2020/08/15 Python
HTML5 Canvas的常用线条属性值总结
2016/03/17 HTML / CSS
购买美国制造的相框和画框架:Picture Frames
2018/08/14 全球购物
JD Sports芬兰:英国领先的运动鞋和运动服饰零售商
2018/11/16 全球购物
美国隐形眼镜零售商:LensPure
2019/03/10 全球购物
Blank NYC官网:夹克、牛仔裤等
2020/12/16 全球购物
如何用Java实现列出某个目录下的所有子目录
2015/07/20 面试题
英文商务邀请信
2014/01/22 职场文书
教师网络培训感言
2014/03/09 职场文书
推广活动策划方案
2014/08/23 职场文书
教师纪念9.18事件演讲稿范文
2014/09/14 职场文书
导游词之河北邯郸
2019/09/12 职场文书
Java基于Dijkstra算法实现校园导游程序
2022/03/17 Java/Android