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计算文本文件行数的方法
Jul 06 Python
python 转换 Javascript %u 字符串为python unicode的代码
Sep 06 Python
Python数据结构与算法之二叉树结构定义与遍历方法详解
Dec 12 Python
Python中支持向量机SVM的使用方法详解
Dec 26 Python
python+selenium 定位到元素,无法点击的解决方法
Jan 30 Python
详解Python中打乱列表顺序random.shuffle()的使用方法
Nov 11 Python
python使用SQLAlchemy操作MySQL
Jan 02 Python
在tensorflow中实现屏蔽输出的log信息
Feb 04 Python
Tkinter中复选菜单是否被选中的判断与设置方式
Mar 04 Python
Pytorch高阶OP操作where,gather原理
Apr 30 Python
使用Keras预训练模型ResNet50进行图像分类方式
May 23 Python
详解anaconda离线安装pytorchGPU版
Sep 08 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
Protoss魔法科技
2020/03/14 星际争霸
PHP 命名空间实例说明
2011/01/27 PHP
PHP学习笔记 IIS7下安装配置php环境
2012/10/29 PHP
浅析php创建者模式
2014/11/25 PHP
解决在laravel中leftjoin带条件查询没有返回右表为NULL的问题
2019/10/15 PHP
js控制frameSet示例
2013/09/10 Javascript
JS 打印界面的CSS居中代码适用所有浏览器
2014/03/19 Javascript
jquery 自定义容器下雨效果可将下雨图标改为其他
2014/04/23 Javascript
js计算任意值之间随机数的方法
2015/01/16 Javascript
JS+CSS实现Div弹出窗口同时背景变暗的方法
2015/03/04 Javascript
js+css实现的圆角边框TAB选项卡滑动门代码分享(2款)
2015/08/26 Javascript
JavaScript学习笔记之取数组中最大值和最小值
2016/03/23 Javascript
JavaScript的instanceof运算符学习教程
2016/06/08 Javascript
vue todo-list组件发布到npm上的方法
2018/04/04 Javascript
详解angular部署到iis出现404解决方案
2018/08/14 Javascript
微信小程序遍历Echarts图表实现多个饼图
2019/04/25 Javascript
Python中pow()和math.pow()函数用法示例
2018/02/11 Python
Python秒算24点实现及原理详解
2019/07/29 Python
Django上使用数据可视化利器Bokeh解析
2019/07/31 Python
一文带你掌握Pyecharts地理数据可视化的方法
2021/02/06 Python
CSS3 Flexbox中flex-shrink属性的用法示例介绍
2013/12/30 HTML / CSS
一文彻底解决HTML5页面中长按保存图片功能
2019/06/10 HTML / CSS
WoolOvers澳洲官方网站:英国针织服装公司
2018/05/13 全球购物
教学大赛获奖感言
2014/01/15 职场文书
中医临床专业自我鉴定范文
2014/01/15 职场文书
幼儿园教师个人反思
2014/01/30 职场文书
警校毕业生自我评价
2014/04/06 职场文书
校长寄语大全
2014/04/09 职场文书
计算机多媒体专业自荐信
2014/07/04 职场文书
国际商务专业求职信
2014/07/15 职场文书
2014年电话客服工作总结
2014/12/09 职场文书
《山中访友》教学反思
2016/02/24 职场文书
会议承办单位欢迎词
2019/07/09 职场文书
go语言基础 seek光标位置os包的使用
2021/05/09 Golang
Python 读取千万级数据自动写入 MySQL 数据库
2022/06/28 Python
Python爬取奶茶店数据分析哪家最好喝以及性价比
2022/09/23 Python