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专用方法与迭代机制实例分析
Sep 15 Python
用Python实现协同过滤的教程
Apr 08 Python
python 链接和操作 memcache方法
Mar 04 Python
详解python中的文件与目录操作
Jul 11 Python
python 对多个csv文件分别进行处理的方法
Jan 07 Python
基于django channel实现websocket的聊天室的方法示例
Apr 11 Python
django创建超级用户过程解析
Sep 18 Python
Django REST framework 单元测试实例解析
Nov 07 Python
python bluetooth蓝牙信息获取蓝牙设备类型的方法
Nov 29 Python
Python MySQLdb 执行sql语句时的参数传递方式
Mar 04 Python
python实现梯度下降算法的实例详解
Aug 17 Python
Python3的进程和线程你了解吗
Mar 16 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使用pdo连接并查询sql数据库的方法
2014/12/24 PHP
php+mysqli数据库连接的两种方式
2015/01/28 PHP
PHP正则匹配反斜杠'\'和美元'$'的方法
2017/02/08 PHP
laravel框架中控制器的创建和使用方法分析
2019/11/23 PHP
PHP超全局变量实现原理及代码解析
2020/09/01 PHP
laravel中Redis队列监听中断的分析
2020/09/14 PHP
再谈IE中Flash控件的自动激活 ObjectWrap
2007/03/09 Javascript
javascript获得当前的信息的一些常用命令
2015/02/25 Javascript
JavaScript SweetAlert插件实现超酷消息警告框
2016/01/28 Javascript
Javascript将JSON日期格式化
2016/08/23 Javascript
Bootstrap缩略图与警告框学习使用
2017/02/08 Javascript
javascript实现二叉树遍历的代码
2017/06/08 Javascript
基于easyui checkbox 的一些操作处理方法
2017/07/10 Javascript
vue.js如何更改默认端口号8080为指定端口的方法
2017/07/14 Javascript
JS原生带小白点轮播图实例讲解
2017/07/22 Javascript
nodejs简单实现TCP服务器端和客户端的聊天功能示例
2018/01/04 NodeJs
vue 下列表侧滑操作实例代码详解
2018/07/24 Javascript
video.js 一个页面同时播放多个视频的实例代码
2018/11/27 Javascript
react-native聊天室|RN版聊天App仿微信实例|RN仿微信界面
2019/11/12 Javascript
你知道JavaScript Symbol类型怎么用吗
2020/01/08 Javascript
JS自定义右键菜单实现代码解析
2020/07/16 Javascript
零基础写python爬虫之爬虫框架Scrapy安装配置
2014/11/06 Python
python使用多线程不断刷新网页的方法
2015/03/31 Python
5种Python单例模式的实现方式
2016/01/14 Python
Python学习小技巧之列表项的拼接
2017/05/20 Python
Python图像处理PIL各模块详细介绍(推荐)
2019/07/17 Python
pycharm下pyqt4安装及环境配置的教程
2020/04/24 Python
HTML5 Canvas绘制文本及图片的基础教程
2016/03/14 HTML / CSS
介绍一下write命令
2012/09/24 面试题
资产经营总监岗位职责范文
2013/12/01 职场文书
财务管理专业求职信
2014/06/11 职场文书
自动化专业毕业生求职信
2014/06/18 职场文书
小学向国旗敬礼活动方案
2014/09/27 职场文书
在职证明格式样本
2015/06/15 职场文书
Python入门之基础语法详解
2021/05/11 Python
django 认证类配置实现
2021/11/11 Python