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读写Redis数据库操作示例
Mar 18 Python
Python的动态重新封装的教程
Apr 11 Python
在ironpython中利用装饰器执行SQL操作的例子
May 02 Python
python编程嵌套函数实例代码
Feb 11 Python
使用Python 正则匹配两个特定字符之间的字符方法
Dec 24 Python
python opencv minAreaRect 生成最小外接矩形的方法
Jul 01 Python
Django框架视图函数设计示例
Jul 29 Python
python 和c++实现旋转矩阵到欧拉角的变换方式
Dec 04 Python
Django3.0 异步通信初体验(小结)
Dec 04 Python
python爬虫模拟浏览器访问-User-Agent过程解析
Dec 28 Python
如何用Matlab和Python读取Netcdf文件
Feb 19 Python
怎么用Python识别手势数字
Jun 07 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 将字符串按大写字母分隔成字符串数组
2010/04/30 PHP
php数字转汉字代码(算法)
2011/10/08 PHP
PHP 之Section与Cookie使用总结
2012/09/14 PHP
如何使用Gitblog和Markdown建自己的博客
2015/07/31 PHP
再推荐十款免费的php开发工具
2015/11/09 PHP
jQuery对表单元素的取值和赋值操作代码
2011/05/19 Javascript
基于jQuery的模仿新浪微博时间的组件
2011/10/04 Javascript
jQuery之选项卡的简单实现
2014/02/28 Javascript
理解javascript对象继承
2016/04/17 Javascript
AngularJS使用自定义指令替代ng-repeat的方法
2016/09/17 Javascript
jQuery中的select操作详解
2016/11/29 Javascript
微信小程序 switch组件详解及简单实例
2017/01/10 Javascript
Vue.js如何实现路由懒加载浅析
2017/08/14 Javascript
妙用缓存调用链实现JS方法的重载
2018/04/30 Javascript
[01:51]2014DOTA2国际邀请赛 这个赛场没有失败者VGTi5再见
2014/07/23 DOTA
Python机器学习之决策树算法
2017/12/22 Python
Python实现JSON反序列化类对象的示例
2018/01/31 Python
python中正则表达式的使用方法
2018/02/25 Python
python:pandas合并csv文件的方法(图书数据集成)
2018/04/12 Python
python3.4实现邮件发送功能
2018/05/28 Python
wtfPython—Python中一组有趣微妙的代码【收藏】
2018/08/31 Python
python3.6使用urllib完成下载的实例
2018/12/19 Python
用Python解决x的n次方问题
2019/02/08 Python
python 实现多维数组(array)排序
2020/02/28 Python
django修改models重建数据库的操作
2020/03/31 Python
html5拖曳操作 HTML5实现网页元素的拖放操作
2013/01/02 HTML / CSS
html5+css如何实现中间大两头小的轮播效果
2018/12/06 HTML / CSS
护理学中专毕业生求职信
2013/11/11 职场文书
人事助理岗位职责
2013/11/18 职场文书
总会计师岗位职责
2014/02/19 职场文书
小学庆六一活动方案
2014/02/28 职场文书
人事部专员岗位职责
2014/03/04 职场文书
爱心捐款感谢信
2015/01/20 职场文书
升职自荐信范文
2015/03/27 职场文书
2016党员党章学习心得体会
2016/01/14 职场文书
logback 实现给变量指定默认值
2021/08/30 Java/Android