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 相关文章推荐
python3实现暴力穷举博客园密码
Jun 19 Python
对Python 网络设备巡检脚本的实例讲解
Apr 22 Python
python实现将汉字保存成文本的方法
Nov 16 Python
对django layer弹窗组件的使用详解
Aug 31 Python
python线程定时器Timer实现原理解析
Nov 30 Python
基于YUV 数据格式详解及python实现方式
Dec 09 Python
如何将你的应用迁移到Python3的三个步骤
Dec 22 Python
Python 实现自动获取种子磁力链接方式
Jan 16 Python
python实现3D地图可视化
Mar 25 Python
Python自定义聚合函数merge与transform区别详解
May 26 Python
pandas DataFrame运算的实现
Jun 14 Python
基于Keras的格式化输出Loss实现方式
Jun 17 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 adodb连接mssql解决乱码问题
2009/06/12 PHP
PHP源码之 ext/mysql扩展部分
2009/07/17 PHP
解析isset与is_null的区别
2013/08/09 PHP
PHP中addslashes()和stripslashes()实现字符串转义和还原用法实例
2016/01/07 PHP
php实现批量上传数据到数据库(.csv格式)的案例
2017/06/18 PHP
javascript之更有效率的字符串替换
2008/08/02 Javascript
javascript小数四舍五入多种方法实现
2012/12/23 Javascript
图片动画横条广告带上下滚动可自定义图片、链接等等
2013/10/20 Javascript
深入浅析同源策略和跨域访问
2015/11/26 Javascript
jquery判断复选框是否选中进行答题提示特效
2015/12/10 Javascript
layui之select的option叠加问题的解决方法
2018/03/08 Javascript
jQuery简单判断值是否存在于数组中的方法示例
2018/04/17 jQuery
Vuejs+vue-router打包+Nginx配置的实例
2018/09/20 Javascript
关于element-ui的隐藏组件el-scrollbar的使用
2019/05/29 Javascript
node.js中Buffer缓冲器的原理与使用方法分析
2019/11/23 Javascript
Python处理RSS、ATOM模块FEEDPARSER介绍
2015/02/18 Python
Python的Django中django-userena组件的简单使用教程
2015/05/30 Python
Python画图学习入门教程
2016/07/01 Python
python实现的正则表达式功能入门教程【经典】
2017/06/05 Python
Python 实现数据库(SQL)更新脚本的生成方法
2017/07/09 Python
itchat和matplotlib的结合使用爬取微信信息的实例
2017/08/25 Python
Python3爬虫爬取英雄联盟高清桌面壁纸功能示例【基于Scrapy框架】
2018/12/05 Python
元组列表字典(莫烦python基础)
2019/04/03 Python
详解使用python绘制混淆矩阵(confusion_matrix)
2019/07/14 Python
python 画3维轨迹图并进行比较的实例
2019/12/06 Python
为什么说python更适合树莓派编程
2020/07/20 Python
python如何对链表操作
2020/10/10 Python
德国鞋子网上商店:Omoda.de
2017/03/31 全球购物
西班牙语在线票务市场:SuperBoletería
2019/06/10 全球购物
金蝶的一道SQL笔试题
2012/12/18 面试题
学生旷课检讨书500字
2014/10/28 职场文书
五年级下册复习计划
2015/01/19 职场文书
党员反四风学习心得体会
2016/01/22 职场文书
2016年保险公众宣传日活动总结
2016/04/05 职场文书
python超详细实现完整学生成绩管理系统
2022/03/17 Python
Linux中一对多配置日志服务器的详细步骤
2022/07/23 Servers