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 相关文章推荐
linux系统使用python获取内存使用信息脚本分享
Jan 15 Python
Python中类的定义、继承及使用对象实例详解
Apr 30 Python
Python中的下划线详解
Jun 24 Python
django实现同一个ip十分钟内只能注册一次的实例
Nov 03 Python
Python使用base64模块进行二进制数据编码详解
Jan 11 Python
PyQt5每天必学之关闭窗口
Apr 19 Python
python实现梯度下降算法
Mar 24 Python
Python实现带参数的用户验证功能装饰器示例
Dec 14 Python
python Elasticsearch索引建立和数据的上传详解
Aug 04 Python
使用python实现滑动验证码功能
Aug 05 Python
Python爬虫requests库多种用法实例
May 28 Python
Python 匹配文本并在其上一行追加文本
May 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
PHP中将字符串转化为整数(int) intval() printf() 性能测试
2020/03/20 PHP
使用PHP计算两个路径的相对路径
2013/06/14 PHP
PHP模拟http请求的方法详解
2016/11/09 PHP
TP5框架简单登录功能实现方法示例
2019/10/31 PHP
SyntaxHighlighter代码加色使用方法
2008/09/07 Javascript
ajax 文件上传应用简单实现
2009/03/03 Javascript
IE下js调试工具Companion.JS
2010/10/15 Javascript
JS使用for循环遍历Table的所有单元格内容
2014/08/21 Javascript
JavaScript中具名函数的多种调用方式总结
2014/11/08 Javascript
判断访客终端类型集锦
2015/06/05 Javascript
JQuery入门基础小实例(1)
2015/09/17 Javascript
JS实现将Asp.Net的DateTime Json类型转换为标准时间的方法
2016/08/02 Javascript
Jquery Easyui搜索框组件SearchBox使用详解(19)
2016/12/17 Javascript
JS实现图片高斯模糊切换效果的焦点图实例
2017/01/21 Javascript
JS如何实现网站中PC端和手机端自动识别并跳转对应的代码
2020/01/08 Javascript
Vant 在vue-cli 4.x中按需加载操作
2020/11/05 Javascript
[01:00:06]加油DOTA_EP01_网络版
2014/08/09 DOTA
Python实现PS滤镜碎片特效功能示例
2018/01/24 Python
python二进制文件的转译详解
2019/07/03 Python
python绘图模块matplotlib示例详解
2019/07/26 Python
Pytorch转onnx、torchscript方式
2020/05/25 Python
python基本算法之实现归并排序(Merge sort)
2020/09/01 Python
利用Python实现学生信息管理系统的完整实例
2020/12/30 Python
国际礼品店:GiftsnIdeas
2018/05/03 全球购物
美国糖果店:Sugarfina
2019/02/21 全球购物
美国最好的葡萄酒网上商店:Wine Library
2019/11/02 全球购物
研发工程师的岗位职责
2013/11/18 职场文书
宿舍打麻将检讨书
2014/01/24 职场文书
最经典的大学生职业生涯规划范文
2014/03/05 职场文书
中国梦口号
2014/06/13 职场文书
文秘班元旦晚会活动策划方案
2014/08/28 职场文书
2014年学校后勤工作总结
2014/12/06 职场文书
房地产置业顾问工作总结
2015/10/23 职场文书
公务员岗前培训心得体会
2016/01/08 职场文书
Redis Cluster集群动态扩容的实现
2021/07/15 Redis
一定要知道的 25 个 Vue 技巧
2021/11/02 Vue.js