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中使用gzip模块压缩文件的简单教程
Apr 08 Python
Google开源的Python格式化工具YAPF的安装和使用教程
May 31 Python
Python中使用装饰器来优化尾递归的示例
Jun 18 Python
Python实现利用最大公约数求三个正整数的最小公倍数示例
Sep 30 Python
python使用正则表达式的search()函数实现指定位置搜索功能
Nov 10 Python
Python实现图片尺寸缩放脚本
Mar 10 Python
Python对List中的元素排序的方法
Apr 01 Python
Python中的并发处理之asyncio包使用的详解
Apr 03 Python
Python中创建二维数组
Oct 17 Python
对python文件读写的缓冲行为详解
Feb 13 Python
python 利用zmail库发送邮件
Sep 11 Python
Python绘制地图神器folium的新人入门指南
May 23 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新手上路(六)
2006/10/09 PHP
php保存信息到当前Session的方法
2015/03/16 PHP
javascript实现的动态文字变换
2007/07/28 Javascript
用Javascript同时提交多个Web表单的方法
2009/12/26 Javascript
javascript 学习笔记(onchange等)
2010/11/14 Javascript
jQuery 源码分析笔记(2) 变量列表
2011/05/28 Javascript
火狐textarea输入法的bug的触发及解决
2013/07/24 Javascript
jQuery避免$符和其他JS库冲突的方法对比
2014/02/20 Javascript
js中的onchange和onpropertychange (onchange无效的解决方法)
2014/03/08 Javascript
动态载入js提高网页打开速度的方法
2014/07/04 Javascript
JavaScript中switch判断容易犯错的一个细节
2014/08/27 Javascript
使用jQueryMobile实现滑动翻页效果的方法
2015/02/04 Javascript
javascript 使用for循环时该注意的问题-附问题总结
2015/08/19 Javascript
基于javascript实现九九乘法表
2016/03/27 Javascript
Jquery on绑定的事件 触发多次实例代码
2016/12/08 Javascript
JQuery获取鼠标进入和离开容器的方向
2016/12/29 Javascript
bootstrap表格分页实例讲解
2016/12/30 Javascript
Vue + Webpack + Vue-loader学习教程之功能介绍篇
2017/03/14 Javascript
JavaScript创建对象的七种方式(推荐)
2017/06/26 Javascript
vue2 全局变量的设置方法
2018/03/09 Javascript
js实现多个倒计时并行 js拼团倒计时
2019/02/25 Javascript
Bootstarp在pycharm中的安装及简单的使用方法
2019/04/19 Javascript
vue组件间通信六种方式(总结篇)
2019/05/15 Javascript
vue使用swiper实现中间大两边小的轮播图效果
2019/11/24 Javascript
nuxt静态部署打包相对路径操作
2020/11/06 Javascript
实例Python处理XML文件的方法
2015/08/31 Python
python 用正则表达式筛选文本信息的实例
2018/06/05 Python
Python二进制文件读取并转换为浮点数详解
2019/06/25 Python
Python列表删除元素del、pop()和remove()的区别小结
2019/09/11 Python
python selenium操作cookie的实现
2020/03/18 Python
北京奥运会主题口号
2014/06/13 职场文书
高中国旗下的演讲稿
2014/08/28 职场文书
矛盾论读书笔记
2015/06/29 职场文书
2016年安全月活动总结
2016/04/06 职场文书
通过T-SQL语句创建游标与实现数据库加解密功能
2022/03/16 SQL Server
css之clearfix的用法深入理解(必看篇)
2023/05/21 HTML / CSS