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 相关文章推荐
centos下更新Python版本的步骤
Feb 12 Python
关于Python中浮点数精度处理的技巧总结
Aug 10 Python
Django+Ajax+jQuery实现网页动态更新的实例
May 28 Python
在pycharm 中添加运行参数的操作方法
Jan 19 Python
python使用Plotly绘图工具绘制柱状图
Apr 01 Python
利用python和百度地图API实现数据地图标注的方法
May 13 Python
在python plt图表中文字大小调节的方法
Jul 08 Python
django框架使用方法详解
Jul 18 Python
如何爬取通过ajax加载数据的网站
Aug 15 Python
python实现监控阿里云账户余额功能
Dec 16 Python
Python 剪绳子的多种思路实现(动态规划和贪心)
Feb 24 Python
Jupyter Notebook 远程访问配置详解
Jan 11 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
DOTA2 玩家自创拉野攻略 特色英雄快速成长篇
2020/04/20 DOTA
php中用date函数获取当前时间有误的解决办法
2013/08/02 PHP
PHP中redis的用法深入解析
2014/02/20 PHP
PHP中可以自动分割查询字符的Parse_str函数使用示例
2014/07/25 PHP
php实现购物车功能(以大苹果购物网为例)
2017/03/09 PHP
PHP钩子与简单分发方式实例分析
2017/09/04 PHP
thinkPHP5框架设置404、403等http状态页面的方法
2018/06/05 PHP
php中文语义分析实现方法示例
2019/09/28 PHP
js实现异步循环实现代码
2016/02/16 Javascript
javaScript知识点总结(必看篇)
2016/06/10 Javascript
最常见的左侧分类菜单栏jQuery实现代码
2016/11/28 Javascript
js仿QQ邮箱收件人选择与搜索功能
2017/02/10 Javascript
JS html时钟制作代码分享
2017/03/03 Javascript
CodeMirror js代码加亮使用总结
2017/03/25 Javascript
JavaScript如何一次性展示几万条数据
2017/03/30 Javascript
使用selenium抓取淘宝的商品信息实例
2018/02/06 Javascript
详解express + mock让前后台并行开发
2018/06/06 Javascript
Vue前端判断数据对象是否为空的实例
2020/09/02 Javascript
Python时间戳与时间字符串互相转换实例代码
2013/11/28 Python
python获取标准北京时间的方法
2015/03/24 Python
Python实现按中文排序的方法示例
2018/04/25 Python
numpy.random模块用法总结
2019/05/27 Python
Python如何基于Tesseract实现识别文字功能
2020/06/05 Python
使用SVG实现提示框功能的示例代码
2020/06/05 HTML / CSS
麦当劳印度网上订餐:McDelivery
2020/03/16 全球购物
Goodee官方商店:迷你投影仪
2021/03/15 全球购物
倡议书范文
2014/04/16 职场文书
个人考核材料
2014/05/15 职场文书
大学生推广普通话演讲稿
2014/09/21 职场文书
离婚协议书范本
2015/01/26 职场文书
入党介绍人考察意见
2015/06/01 职场文书
创业分两种人:那么哪些适合创业?,哪些适合不适合创业呢?
2019/08/23 职场文书
使用Nginx搭载rtmp直播服务器的方法
2021/10/16 Servers
阿里云日志过滤器配置日志服务
2022/04/09 Servers
零基础学java之带返回值的方法的定义和调用
2022/04/10 Java/Android
windows10声卡驱动怎么安装?win10声卡驱动安装操作步骤教程
2022/08/05 数码科技