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标准日志模块logging的使用方法
Nov 01 Python
python实现进程间通信简单实例
Jul 23 Python
Python中动态获取对象的属性和方法的教程
Apr 09 Python
django接入新浪微博OAuth的方法
Jun 29 Python
Python实现多线程HTTP下载器示例
Feb 11 Python
Linux RedHat下安装Python2.7开发环境
May 20 Python
Python中Numpy包的安装与使用方法简明教程
Jul 03 Python
centos6.8安装python3.7无法import _ssl的解决方法
Sep 17 Python
python xlwt如何设置单元格的自定义背景颜色
Sep 03 Python
Pycharm创建文件时自动生成文件头注释(自定义设置作者日期)
Nov 24 Python
python实现杨辉三角的几种方法代码实例
Mar 02 Python
python 如何用map()函数创建多线程任务
Apr 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
thinkphp5实现无限级分类
2019/02/18 PHP
PHP工厂模式的日常使用
2019/03/20 PHP
Laravel框架集成UEditor编辑器的方法图文与实例详解
2019/04/17 PHP
JavaScript 对象模型 执行模型
2010/10/15 Javascript
javascript读写XML实现广告轮换(兼容IE、FF)
2013/08/09 Javascript
莱鸟介绍window.print()方法
2016/01/06 Javascript
JavaScript实现输入框(密码框)出现提示语
2016/01/12 Javascript
基于JS实现类似支付宝支付密码输入框
2016/09/02 Javascript
JavaScript获取短信验证码(周期性)
2016/12/29 Javascript
JS实现页面打印(整体、局部)
2017/08/18 Javascript
vue对storejs获取的数据进行处理时遇到的几种问题小结
2018/03/20 Javascript
详解VueJs中的V-bind指令
2018/05/03 Javascript
微信小程序入口场景的问题集合与相关解决方法
2019/06/26 Javascript
微信小程序使用蓝牙小插件
2019/09/23 Javascript
vue使用map代替Aarry数组循环遍历的方法
2020/04/30 Javascript
解决vue组件销毁之后计时器继续执行的问题
2020/07/21 Javascript
[39:19]完美世界DOTA2联赛PWL S2 SZ vs LBZS 第二场 11.26
2020/11/30 DOTA
使用Protocol Buffers的C语言拓展提速Python程序的示例
2015/04/16 Python
django模板结构优化的方法
2019/02/28 Python
Python的matplotlib绘图如何修改背景颜色的实现
2019/07/16 Python
Django密码存储策略分析
2020/01/09 Python
解决paramiko执行命令超时的问题
2020/04/16 Python
Python如何读写CSV文件
2020/08/13 Python
jupyter notebook 写代码自动补全的实现
2020/11/02 Python
利用CSS3实现进度条的两种姿势详解
2017/03/21 HTML / CSS
H5调用相机拍照并压缩图片的实例代码
2017/07/20 HTML / CSS
美国珠宝网上商店:Jeulia
2016/09/01 全球购物
史蒂夫·马登加拿大官网:Steve Madden加拿大
2017/11/18 全球购物
英国在线电子和小工具商店:TecoBuy
2018/10/06 全球购物
大学生学习2014全国两会心得体会
2014/03/13 职场文书
见习期自我鉴定范文
2014/03/19 职场文书
医院党的群众路线教育实践活动领导班子对照检查材料
2014/09/25 职场文书
文明单位汇报材料
2014/12/24 职场文书
国庆节慰问信
2015/02/15 职场文书
创业方案:赚钱的烧烤店该怎样做?
2019/07/05 职场文书
关于Mybatis中SQL节点的深入解析
2022/03/19 Java/Android