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中有趣在__call__函数
Jun 21 Python
python奇偶行分开存储实现代码
Mar 19 Python
python 异或加密字符串的实例
Oct 14 Python
python 使用装饰器并记录log的示例代码
Jul 12 Python
Django之提交表单与前后端交互的方法
Jul 19 Python
Python 使用指定的网卡发送HTTP请求的实例
Aug 21 Python
Python使用random模块生成随机数操作实例详解
Sep 17 Python
tensorflow没有output结点,存储成pb文件的例子
Jan 04 Python
Python如何实现在字符串里嵌入双引号或者单引号
Mar 02 Python
python 删除excel表格重复行,数据预处理操作
Jul 06 Python
Pycharm Plugins加载失败问题解决方案
Nov 28 Python
Python OpenCV超详细讲解读取图像视频和网络摄像头
Apr 02 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索引问题
2008/06/07 PHP
ThinkPHP 3.2.3实现页面静态化功能的方法详解
2017/08/03 PHP
JS原型对象通俗"唱法"
2012/12/27 Javascript
jQuery判断密码强度实现思路及代码
2013/04/24 Javascript
JavaScript将Web页面内容导出到Word及Excel的方法
2015/02/13 Javascript
jquery实现简单的无缝滚动
2015/04/15 Javascript
jquery简单实现网页层的展开与收缩效果
2015/08/07 Javascript
jQuery鼠标经过方形图片切换成圆边效果代码分享
2015/08/20 Javascript
js实现文字在按钮上滚动的方法
2015/08/20 Javascript
jQuery的ajax下载blob文件
2016/07/21 Javascript
jQuery 自定义下拉框(DropDown)附源码下载
2016/07/22 Javascript
Javascrip实现文字跳动特效
2016/11/27 Javascript
jQuery点击弹出层弹出模态框点击模态框消失代码分享
2017/01/21 Javascript
Javascript实现购物车功能的详细代码
2018/05/08 Javascript
vue中的watch监听数据变化及watch中各属性的详解
2018/09/11 Javascript
对angular2中的ngfor和ngif指令嵌套实例讲解
2018/09/12 Javascript
微信小程序实现带缩略图轮播效果
2018/11/04 Javascript
利用js-cookie实现前端设置缓存数据定时失效
2019/06/18 Javascript
Node.js系列之发起get/post请求(2)
2019/08/30 Javascript
详细介绍解决vue和jsp结合的方法
2020/02/06 Javascript
sublime text 3配置使用python操作方法
2017/06/11 Python
Python中如何优雅的合并两个字典(dict)方法示例
2017/08/09 Python
python删除过期log文件操作实例解析
2018/01/31 Python
Python pyinotify模块实现对文档的实时监控功能方法
2018/10/13 Python
在Pycharm中执行scrapy命令的方法
2019/01/16 Python
Django 请求Request的具体使用方法
2019/11/11 Python
selenium+python实现基本自动化测试的示例代码
2021/01/27 Python
餐饮收银员岗位职责
2014/02/07 职场文书
助理政工师申报材料
2014/06/03 职场文书
老公婚前保证书
2015/02/28 职场文书
党委工作总结2015
2015/04/27 职场文书
海洋天堂观后感
2015/06/05 职场文书
扩展多台相同的Web服务器
2021/04/01 Servers
MySQL中的布尔值,怎么存储false或true
2021/06/04 MySQL
nginx之queue的具体使用
2022/06/28 Servers
HTML5页面打开微信小程序功能实现
2022/09/23 HTML / CSS