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内置的模块与函数进行不同进制的数的转换
Mar 12 Python
python初学之用户登录的实现过程(实例讲解)
Dec 23 Python
Selenium 模拟浏览器动态加载页面的实现方法
May 16 Python
python opencv3实现人脸识别(windows)
May 25 Python
python求质数的3种方法
Sep 28 Python
使用Python进行目录的对比方法
Nov 01 Python
pyqt5中QThread在使用时出现重复emit的实例
Jun 21 Python
python如何实现异步调用函数执行
Jul 08 Python
Python列表(list)所有元素的同一操作解析
Aug 01 Python
Pycharm生成可执行文件.exe的实现方法
Jun 02 Python
详解torch.Tensor的4种乘法
Sep 03 Python
python基础入门之普通操作与函数(三)
Jun 13 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学习之PHP运算符
2006/10/09 PHP
PHP高级OOP技术演示
2009/08/27 PHP
php小型企业库存管理系统的设计与实现代码
2011/05/16 PHP
基于flush()不能按顺序输出时的解决办法
2013/06/29 PHP
php绘图之在图片上写中文和英文的方法
2015/01/24 PHP
smarty循环嵌套用法示例分析
2016/07/19 PHP
PHP实现的只保留字符串首尾字符功能示例【隐藏部分字符串】
2019/03/11 PHP
PHP实现的操作数组类库定义与用法示例
2019/05/24 PHP
js下用eval生成JSON对象
2010/09/17 Javascript
javascript中负数算术右移、逻辑右移的奥秘探索
2013/10/17 Javascript
js 操作select与option(示例讲解)
2013/12/20 Javascript
js判断字符是否是汉字的两种方法小结
2014/01/03 Javascript
javascript创建和存储cookie示例
2014/01/07 Javascript
jQuery中Dom的基本操作小结
2014/01/23 Javascript
Node.js入门教程:在windows和Linux上安装配置Node.js图文教程
2014/08/14 Javascript
nodejs中实现阻塞实例
2015/03/24 NodeJs
js中遍历对象的属性和值的方法
2016/07/27 Javascript
mui back 返回刷新页面的实例
2017/12/06 Javascript
解决vue脚手架项目打包后路由视图不显示的问题
2018/09/20 Javascript
HTML+JavaScript实现扫雷小游戏
2019/09/30 Javascript
python实现在cmd窗口显示彩色文字
2019/06/24 Python
python正则-re的用法详解
2019/07/28 Python
解析python实现Lasso回归
2019/09/11 Python
django框架基于queryset和双下划线的跨表查询操作详解
2019/12/11 Python
HTML5单页面手势滑屏切换原理分析
2017/07/10 HTML / CSS
100%植物性、有机、即食餐:Sakara Life
2018/10/25 全球购物
德国珠宝和手表在线商店:VALMANO
2019/03/24 全球购物
专科毕业生自我鉴定
2013/12/01 职场文书
终止劳动合同协议书
2014/04/14 职场文书
工厂标语大全
2014/10/06 职场文书
三八妇女节标语
2014/10/09 职场文书
关于召开会议的通知
2015/04/15 职场文书
学校教学工作总结2015
2015/05/19 职场文书
小学六年级毕业感言
2015/07/30 职场文书
详解Django的MVT设计模式
2021/04/29 Python
一文搞懂Java中的注解和反射
2022/06/21 Java/Android