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 29 Python
Python 中 Meta Classes详解
Feb 13 Python
python3结合openpyxl库实现excel操作的实例代码
Sep 11 Python
python获取中文字符串长度的方法
Nov 14 Python
使用 Visual Studio Code(VSCode)搭建简单的Python+Django开发环境的方法步骤
Dec 17 Python
python使用PyQt5的简单方法
Feb 27 Python
在Pytorch中使用样本权重(sample_weight)的正确方法
Aug 17 Python
Python 过滤错误log并导出的实例
Dec 26 Python
在Ubuntu 20.04中安装Pycharm 2020.1的图文教程
Apr 30 Python
使用Keras预训练模型ResNet50进行图像分类方式
May 23 Python
基于Python-turtle库绘制路飞的草帽骷髅旗、美国队长的盾牌、高达的源码
Feb 18 Python
Flask搭建一个API服务器的步骤
May 28 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使用glob函数遍历文件和目录详解
2016/09/23 PHP
php 浮点数比较方法详解
2017/05/05 PHP
PHP基于imagick扩展实现合成图片的两种方法【附imagick扩展下载】
2017/11/14 PHP
php 利用socket发送GET,POST请求的实例代码
2020/07/04 PHP
在textarea中显示html页面的javascript代码
2007/04/20 Javascript
JQuery Easyui Tree的oncheck事件实现代码
2010/05/28 Javascript
JavaScript控制各种浏览器全屏模式的方法、属性和事件介绍
2014/04/03 Javascript
jQuery+canvas实现简单的球体斜抛及颜色动态变换效果
2016/01/28 Javascript
解决JS组件bootstrap table分页实现过程中遇到的问题
2016/04/21 Javascript
JS函数的定义与调用方法推荐
2016/05/12 Javascript
详解js中常规日期格式处理、月历渲染和倒计时函数
2016/12/28 Javascript
React Native之TextInput组件解析示例
2017/08/22 Javascript
vue脚手架搭建过程图解
2018/06/06 Javascript
JS前端知识点offset,scroll,client,冒泡,事件对象的应用整理总结
2019/06/27 Javascript
Vue之beforeEach非登录不能访问的实现(代码亲测)
2019/07/18 Javascript
JS变量提升原理与用法实例浅析
2020/05/22 Javascript
[05:04]完美世界携手游戏风云打造 卡尔工作室地图界面篇
2013/04/23 DOTA
Python中的二叉树查找算法模块使用指南
2014/07/04 Python
python持久性管理pickle模块详细介绍
2015/02/18 Python
Python常用算法学习基础教程
2017/04/13 Python
Python 实现简单的shell sed替换功能(实例讲解)
2017/09/29 Python
Python 实现自动获取种子磁力链接方式
2020/01/16 Python
pycharm实现在虚拟环境中引入别人的项目
2020/03/09 Python
python对XML文件的操作实现代码
2020/03/27 Python
Python多线程threading创建及使用方法解析
2020/06/17 Python
用python写爬虫简单吗
2020/07/28 Python
Python tkinter之Bind(绑定事件)的使用示例
2021/02/05 Python
印度尼西亚综合购物网站:Lazada印尼
2016/09/07 全球购物
MIXIT官网:俄罗斯最大的化妆品公司之一
2020/01/25 全球购物
学生请假条格式
2014/04/11 职场文书
小学阳光体育活动总结
2014/07/05 职场文书
公司员工离职证明书
2014/10/04 职场文书
九寨沟导游词
2015/02/02 职场文书
2016毕业实习单位评语大全
2015/12/01 职场文书
2016年优秀共青团员事迹材料
2016/02/25 职场文书
python glom模块的使用简介
2021/04/13 Python