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中isnumeric()方法的使用简介
May 19 Python
使用Python简单的实现树莓派的WEB控制
Feb 18 Python
python 匹配url中是否存在IP地址的方法
Jun 04 Python
Flask入门之上传文件到服务器的方法示例
Jul 18 Python
python2.7实现邮件发送功能
Dec 12 Python
python实现连续图文识别
Dec 18 Python
基于python历史天气采集的分析
Feb 14 Python
Django单元测试工具test client使用详解
Aug 02 Python
六种酷炫Python运行进度条效果的实现代码
Jul 17 Python
Python爬取豆瓣数据实现过程解析
Oct 27 Python
python脚本定时发送邮件
Dec 22 Python
详解Pycharm第三方库的安装及使用方法
Dec 29 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
4月1日重磅发布!《星际争霸II》6.0.0版本更新
2020/04/09 星际争霸
php截取字符串之截取utf8或gbk编码的中英文字符串示例
2014/03/12 PHP
PHP中使用循环实现的金字塔图形
2014/11/08 PHP
PHP正则验证Email的方法
2015/06/15 PHP
php解析base64数据生成图片的方法
2016/12/06 PHP
PHP给源代码加密的几种方法汇总(推荐)
2018/02/06 PHP
js中的window.open返回object的错误的解决方法
2009/08/15 Javascript
JavaScript学习点滴 call、apply的区别
2010/10/22 Javascript
javascript创建数组之联合数组的使用方法示例
2013/12/26 Javascript
javascript使用百度地图api和html5特性获取浏览器位置
2014/01/10 Javascript
jQuery动画特效实例教程
2014/08/29 Javascript
javascript实现根据3原色制作颜色选择器的方法
2015/07/17 Javascript
jquery SweetAlert插件实现响应式提示框
2015/08/18 Javascript
初步了解javascript面向对象
2015/11/09 Javascript
jQuery实现内容定时切换效果完整实例
2016/04/06 Javascript
html+js+highcharts绘制圆饼图表的简单实例
2016/08/04 Javascript
高效的jQuery代码编写技巧总结
2017/02/22 Javascript
jquery仿微信聊天界面
2017/05/06 jQuery
vue+springmvc导出excel数据的实现代码
2018/06/27 Javascript
vue实现分页组件
2020/06/16 Javascript
Python设计模式之代理模式简单示例
2018/01/09 Python
Python 读取串口数据,动态绘图的示例
2019/07/02 Python
python交互模式下输入换行/输入多行命令的方法
2019/07/02 Python
pytorch实现seq2seq时对loss进行mask的方式
2020/02/18 Python
澳大利亚便宜隐形眼镜购买网站:QUICKLENS Australia
2018/10/06 全球购物
卡骆驰英国官网:Crocs英国
2019/08/22 全球购物
四年的大学生生活自我评价
2013/12/09 职场文书
村委会主任先进事迹
2014/01/15 职场文书
《苏珊的帽子》教学反思
2014/04/07 职场文书
百年孤独读书笔记
2015/06/29 职场文书
2016七一建党节慰问信
2015/11/30 职场文书
不要在HTML中滥用div
2021/05/08 HTML / CSS
科普 | 业余无线电知识-波段篇
2022/02/18 无线电
MySQL 数据 data 基本操作
2022/05/04 MySQL
Redis基本数据类型String常用操作命令
2022/06/01 Redis
win10电脑双屏显示一个黑屏怎么办?win10电脑双屏显示一个黑屏解决方法
2022/07/15 数码科技