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的更多相关文章

  1. CodeIgniter框架——创建一个简单的Web站点(include MySQL基本操作)

    目标 使用 CodeIgniter 创建一个简单的 Web 站点.该站点将有一个主页,显示一些宣传文本和一个表单,该表单将发布到数据库表中. 按照 CodeIgniter 的术语,可将这些需求转换为以 ...

  2. 【Django】01_创建一个简单的项目

    1.创建Django项目 点击:file-->new project,出现下面的对话框.选择Django栏目,输入项目名称, 选择python解释器版本,点击create创建. Django将自 ...

  3. 如何创建一个简单的C++同步锁框架(译)

    翻译自codeproject上面的一篇文章,题目是:如何创建一个简单的c++同步锁框架 目录 介绍 背景 临界区 & 互斥 & 信号 临界区 互斥 信号 更多信息 建立锁框架的目的 B ...

  4. 用django创建一个简单的sns

    用django创建一个简单的sns 1.首先创建一个工程newsns django-admin.py startproject newsns 在工程目录下新建一个文件夹templates,在该文件夹下 ...

  5. Python框架学习之用Flask创建一个简单项目

    在前面一篇讲了如何创建一个虚拟环境,今天这一篇就来说说如何创建一个简单的Flask项目.关于Flask的具体介绍就不详细叙述了,我们只要知道它非常简洁.灵活和扩展性强就够了.它不像Django那样集成 ...

  6. django创建一个简单的web站点

    一.新建project 使用Pycharm,File->New Project…,选择Django,给project命名 (project不能用test命名)   新建的project目录如下: ...

  7. 一个先进的App框架:使用Ionic创建一个简单的APP

    原文  http://www.w3cplus.com/mobile/building-simple-app-using-ionic-advanced-html5-mobile-app-framewor ...

  8. BitAdminCore框架应用篇:(二)创建一个简单的增删改查模块

    NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/cookie ...

  9. 《Entity Framework 6 Recipes》翻译系列 (3) -----第二章 实体数据建模基础之创建一个简单的模型

    第二章 实体数据建模基础 很有可能,你才开始探索实体框架,你可能会问“我们怎么开始?”,如果你真是这样的话,那么本章就是一个很好的开始.如果不是,你已经建模,并在实体分裂和继承方面感觉良好,那么你可以 ...

随机推荐

  1. ABAP 新语法-实例讲解

    主要内容 内联声明 构造表达式 内表操作 Open SQL 其他 本文列出了ABAP新语法的一些使用方式,供大家学习参考. 内联声明 代码实现: *&--------------------- ...

  2. 最简单取得外网IP

    #coding=utf-8import urllib2import re print re.search('\d+\.\d+\.\d+\.\d+',urllib2.urlopen("http ...

  3. Python requests库的使用(二)

    1.请求异常处理 请求异常类型: 请求超时处理(timeout): 实现代码: import requestsfrom requests import exceptions        #引入exc ...

  4. python网络编程-2

    1.理解相关概念 #浅显理解下 对比cpu与io的差距如:io从硬盘读取一条数据9ms ,cpu在9ms可以做450万次指令 cpu切换上下文的方式:1.遇到io操作切换cpu 2.cpu时间片分配 ...

  5. 安卓微信对接H5微信支付出现“商家参数有误,请联系商家解决”的问题处理

    最近遇到客户在对接我们微信支付的时候,一些商家反馈在用户支付的过程中会出现报错,出错的截图如下: 查看微信官方文档如下:https://pay.weixin.qq.com/wiki/doc/api/H ...

  6. i春秋四周年中奖名单出炉丨确认过眼神,你是中奖人

    i春秋四周年任性狂欢倒计时最后2天! 优享会员.精品课程.CTF经典赛题实战班.Web安全线上提高班.渗透测试工程师线下就业班.CISAW-Web安全认证......全部史上最低折扣,还有8888元现 ...

  7. python部分笔记

    创建类和对象 图片转在自:https://www.cnblogs.com/aylin/p/5547558.html 图片转在自:https://www.cnblogs.com/aylin/p/5547 ...

  8. iSCSI的配置(target/initiator)

    iSCSI:Internet 小型计算机系统接口 (iSCSI:Internet Small Computer System Interface) iSCSI技术是一种由IBM公司研究开发的,是一个供 ...

  9. JAVA中为什么要配置环境变量?怎么配置环境变量?

    1.为什么要配置环境变量? 答:为了让javac命令(编译命令)和Java命令(运行命令)能在任何文件夹都能运行. 2.怎么配置环境变量? JAVA_HOME : D:\develop\Java\jd ...

  10. MySQL添加CSV文件中的数据

    一.MySQL添加csv数据 此问题是前几天整理数据的时候碰到的,数据存在 CSV文件中(200多万记录),通过python 往数据库中导入太慢了,后来使用MySQL 中自带的命令 LOAD DATA ...