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中运算符使用时的优先级
May 14 Python
Python实现TCP协议下的端口映射功能的脚本程序示例
Jun 14 Python
python3使用PyMysql连接mysql数据库实例
Feb 07 Python
Python 转义字符详细介绍
Mar 21 Python
Python装饰器的执行过程实例分析
Jun 04 Python
python调用摄像头显示图像的实例
Aug 03 Python
Python读写文件基础知识点
Jun 10 Python
在python中用print()输出多个格式化参数的方法
Jul 16 Python
django 2.2和mysql使用的常见问题
Jul 18 Python
python实现PDF中表格转化为Excel的方法
Jun 16 Python
keras训练浅层卷积网络并保存和加载模型实例
Jul 02 Python
Python实现http接口自动化测试的示例代码
Oct 09 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
怎么使 Mysql 数据同步
2006/10/09 PHP
php使用cookie保存用户登录的用户名实例
2015/01/26 PHP
php高清晰度无损图片压缩功能的实现代码
2018/12/09 PHP
google地图的路线实现代码
2009/08/20 Javascript
JavaScript的public、private和privileged模式
2009/12/28 Javascript
使用javascript实现ListBox左右全选,单选,多选,全请
2013/11/07 Javascript
jQuery 删除/替换DOM元素的几种方式
2014/05/20 Javascript
使用node.js 获取客户端信息代码分享
2014/11/26 Javascript
JavaScript 实现打印,打印预览,打印设置
2014/12/30 Javascript
jQuery实现点击某个div打开层,点击其他div关闭层实例分析(阻止冒泡)
2016/11/18 Javascript
jQuery实现验证码功能
2017/03/17 Javascript
nodejs body-parser 解析post数据实例
2017/07/26 NodeJs
微信小程序实现顶部普通选项卡效果(非swiper)
2020/06/19 Javascript
如何自动化部署项目?折腾服务器之旅~
2019/04/16 Javascript
微信小程序自定义菜单切换栏tabbar组件代码实例
2019/12/30 Javascript
[01:11]辉夜杯战队访谈宣传片—CDEC.Y
2015/12/26 DOTA
haskell实现多线程服务器实例代码
2013/11/26 Python
Python基于有道实现英汉字典功能
2015/07/25 Python
python实现备份目录的方法
2015/08/03 Python
Python图算法实例分析
2016/08/13 Python
Python 自动化表单提交实例代码
2017/06/08 Python
Django ORM框架的定时任务如何使用详解
2017/10/19 Python
python版学生管理系统
2018/01/10 Python
[原创]windows下Anaconda的安装与配置正解(Anaconda入门教程)
2018/04/05 Python
Python爬虫设置代理IP(图文)
2018/12/23 Python
Django 多环境配置详解
2019/05/14 Python
python GUI库图形界面开发之PyQt5信号与槽基本操作
2020/02/25 Python
Python基于QQ邮箱实现SSL发送
2020/04/26 Python
python实现简易版学生成绩管理系统
2020/06/22 Python
CSS3的RGBA中关于整数和百分比值的转换
2015/08/04 HTML / CSS
舞蹈专业大学生职业规划范文
2014/03/12 职场文书
拉贝日记观后感
2015/06/05 职场文书
七年级思品教学反思
2016/02/20 职场文书
Django如何创作一个简单的最小程序
2021/05/12 Python
Python 的 sum() Pythonic 的求和方法详细
2021/10/16 Python
关于PHP数组迭代器的使用方法实例
2021/11/17 PHP