使用Django REST框架创建一个简单的Api
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:

- 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/

References:
Create a Simple API Using Django REST Framework in Python
使用Django REST框架创建一个简单的Api的更多相关文章
- CodeIgniter框架——创建一个简单的Web站点(include MySQL基本操作)
目标 使用 CodeIgniter 创建一个简单的 Web 站点.该站点将有一个主页,显示一些宣传文本和一个表单,该表单将发布到数据库表中. 按照 CodeIgniter 的术语,可将这些需求转换为以 ...
- 【Django】01_创建一个简单的项目
1.创建Django项目 点击:file-->new project,出现下面的对话框.选择Django栏目,输入项目名称, 选择python解释器版本,点击create创建. Django将自 ...
- 如何创建一个简单的C++同步锁框架(译)
翻译自codeproject上面的一篇文章,题目是:如何创建一个简单的c++同步锁框架 目录 介绍 背景 临界区 & 互斥 & 信号 临界区 互斥 信号 更多信息 建立锁框架的目的 B ...
- 用django创建一个简单的sns
用django创建一个简单的sns 1.首先创建一个工程newsns django-admin.py startproject newsns 在工程目录下新建一个文件夹templates,在该文件夹下 ...
- Python框架学习之用Flask创建一个简单项目
在前面一篇讲了如何创建一个虚拟环境,今天这一篇就来说说如何创建一个简单的Flask项目.关于Flask的具体介绍就不详细叙述了,我们只要知道它非常简洁.灵活和扩展性强就够了.它不像Django那样集成 ...
- django创建一个简单的web站点
一.新建project 使用Pycharm,File->New Project…,选择Django,给project命名 (project不能用test命名) 新建的project目录如下: ...
- 一个先进的App框架:使用Ionic创建一个简单的APP
原文 http://www.w3cplus.com/mobile/building-simple-app-using-ionic-advanced-html5-mobile-app-framewor ...
- BitAdminCore框架应用篇:(二)创建一个简单的增删改查模块
NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/cookie ...
- 《Entity Framework 6 Recipes》翻译系列 (3) -----第二章 实体数据建模基础之创建一个简单的模型
第二章 实体数据建模基础 很有可能,你才开始探索实体框架,你可能会问“我们怎么开始?”,如果你真是这样的话,那么本章就是一个很好的开始.如果不是,你已经建模,并在实体分裂和继承方面感觉良好,那么你可以 ...
随机推荐
- XtraReport报表入库单数字转中文大写数字
先看看打印入库单的效果图,看如下: 客户要求合计一行,要求大写中文数字.XtraReport报表是如何做出以上图的效果呢?因为是要把数字转成大写中文数字,得先准备数字转大写中文数字的函数.因网上有很多 ...
- java进销存管理系统的设计与实现-springboot源码
开发环境: Windows操作系统 开发工具:MyEclipse/Eclipse + JDK+ Tomcat + MySQL 数据库 项目简介: 系统前段页面采用jsp + JavaScrip ...
- 四.Windows基础
系统目录 Windows Program files 用户 Perflogs:是Windows7的日志信息,如磁盘扫描错误信息,删掉可以,但不建议删,删掉反而降低系统速度,perflogs是系统自动生 ...
- 相同域名下的cookie污染
问题描述 本地用同一个tomcat调试两个相同框架的不同项目,在同一个浏览器界面里切换时,A项目的登录会把B项目的登录给踢掉,翻反过来亦如此.通过查看浏览器cookie,发现两个项目的cookie完全 ...
- Jetty启动报Error scanning entry META-INF/versions/9/org/apache/logging/log4j/util/ProcessIdUtil.class
近日在项目中集成Elasticsearch后,Jetty启动报错. 错误日志如下: Suppressed: |java.lang.RuntimeException: Error scanning en ...
- The listener supports no services oracle注册监听
问题登场: [oracle@my-e450 ~]$ lsnrctl status …… The listener supports no servicesThe command completed s ...
- MySQL中建立Oracle中SCOTT数据示例
1.创建 scott 数据库 CREATE DATABASE scott; 2.创建 scott 数据库中的 dept 表 CREATE TABLE dept( deptno INT UNSIGNED ...
- 曹玉中-201871010105《面向对象程序设计(java)》第6-7周学习总结
曹玉中-201871010105<面向对象程序设计(java)>第6-7周学习总结 项目 内容 这个作业属于哪个课程 <任课教师博客主页链接> https://www.c ...
- redis常用命令手册大全
一.五种数据类型1.Redis字符串StringString 是最简单的类型,你可以理解成与 Memcached 是一模一样的类型,一个 key 对应一个value,其上支持的操作与 Memcache ...
- Slack完整教学与上手心得:找到正确的团队沟通之道
Slack完整教学与上手心得:找到正确的团队沟通之道 时间 2015-06-13 09:21:42 逐鹿网 原文 http://www.zhulu.com/article/5519.html 主题 ...