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 相关文章推荐
scrapy爬虫完整实例
Jan 25 Python
python单例模式实例解析
Aug 28 Python
对pandas写入读取h5文件的方法详解
Dec 28 Python
Python小游戏之300行代码实现俄罗斯方块
Jan 04 Python
Python3.5文件读与写操作经典实例详解
May 01 Python
Python 在OpenCV里实现仿射变换—坐标变换效果
Aug 30 Python
PYTHON EVAL的用法及注意事项解析
Sep 06 Python
如何利用Python开发一个简单的猜数字游戏
Sep 22 Python
python3.7 openpyxl 删除指定一列或者一行的代码
Oct 08 Python
python标准库os库的函数介绍
Feb 12 Python
使用Keras构造简单的CNN网络实例
Jun 29 Python
Python批量获取并保存手机号归属地和运营商的示例
Oct 09 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 mysql procedure实现获取多个结果集的方法【基于thinkPHP】
2016/11/09 PHP
php引用和拷贝的区别知识点总结
2019/09/23 PHP
Apache站点配置SSL强制跳转443
2021/03/09 Servers
Bootstrap中CSS的使用方法
2016/02/17 Javascript
BootStrap使用file-input插件上传图片的方法
2016/09/05 Javascript
用jquery快速解决IE输入框不能输入的问题
2016/10/04 Javascript
input输入框内容实时监测(附代码)
2017/08/15 Javascript
解决Vue-cli npm run build生产环境打包,本地不能打开的问题
2018/09/20 Javascript
vue 项目build错误异常的解决方法
2019/04/22 Javascript
JavaScript跳出循环的三种方法(break, return, continue)
2019/07/30 Javascript
vue 实现v-for循环回来的数据动态绑定id
2019/11/07 Javascript
JavaScript接口实现方法实例分析
2020/05/16 Javascript
[01:50]WODOTA制作 DOTA2中文宣传片《HERO》
2013/04/28 DOTA
[03:42]2014DOTA2国际邀请赛 第三日比赛排位扑朔迷离
2014/07/12 DOTA
python备份文件的脚本
2008/08/11 Python
Python实现的百度站长自动URL提交小工具
2014/06/27 Python
编写Python CGI脚本的教程
2015/06/29 Python
python实现文本去重且不打乱原本顺序
2016/01/26 Python
详解python发送各类邮件的主要方法
2016/12/22 Python
Django入门使用示例
2017/12/12 Python
基于Python的PIL库学习详解
2019/05/10 Python
python multiprocessing模块用法及原理介绍
2019/08/20 Python
Python PyInstaller库基本使用方法分析
2019/12/12 Python
python两个list[]相加的实现方法
2020/09/23 Python
Python APScheduler执行使用方法详解
2020/12/10 Python
耐克波兰官方网站:Nike波兰
2019/09/03 全球购物
药学专业个人的自我评价
2013/12/31 职场文书
初婚初育证明
2014/01/14 职场文书
企业晚会策划方案
2014/05/29 职场文书
农民工讨薪标语
2014/06/26 职场文书
体育教师个人工作总结
2015/02/09 职场文书
学困生转化工作总结
2015/08/13 职场文书
分布式锁为什么要选择Zookeeper而不是Redis?看完这篇你就明白了
2021/05/21 Redis
解决Pytorch中关于model.eval的问题
2021/05/22 Python
Lakehouse数据湖并发控制陷阱分析
2022/03/31 Oracle
Golang ort 中的sortInts 方法
2022/04/24 Golang