Django REST框架创建一个简单的Api实例讲解

作者:太阳以西 时间:2023-04-28 01:02:40 

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

来源:https://www.cnblogs.com/zhichun/p/11797759.html

标签:Django,REST框架,Api
0
投稿

猜你喜欢

  • numpy.transpose对三维数组的转置方法

    2023-10-11 07:32:36
  • 对MySQL慢查询日志进行分析的基本教程

    2024-01-22 20:32:35
  • django 中的聚合函数,分组函数,F 查询,Q查询

    2021-07-24 02:37:50
  • 使用npm命令提示: 'npm' 不是内部或外部命令,也不是可运行的程序的处理方法

    2024-05-13 09:29:36
  • Python用dilb提取照片上人脸的示例

    2021-07-04 23:34:47
  • Go 语言中 20 个占位符的整理

    2024-04-23 09:45:21
  • python3使用QQ邮箱发送邮件

    2023-09-05 05:16:15
  • 如何在Vue单页面中进行业务数据的上报

    2024-04-26 17:42:13
  • 每个ASP程序员必备的知识

    2008-09-21 21:34:00
  • vue切换页面(路由)时如何保持滚动条回到顶部

    2024-05-28 15:54:49
  • Viso 2019 下载与激活方法

    2023-03-19 08:17:44
  • python知识:装饰器@property到底有啥用途

    2022-07-23 12:25:45
  • 详解CSS的优先权

    2008-05-11 18:57:00
  • TensorFlow 模型载入方法汇总(小结)

    2022-11-09 00:05:42
  • 编写Python的web框架中的Model的教程

    2022-09-29 22:54:19
  • Python常用库推荐

    2023-03-22 09:25:58
  • 浅谈Python 递归算法指归

    2023-01-12 06:27:50
  • Python实现简单查找最长子串功能示例

    2023-01-14 01:20:33
  • BeautifulSoup中find和find_all的使用详解

    2023-11-08 21:00:22
  • Pyscript使用本地Pyodide配置步骤

    2021-06-12 06:58:49
  • asp之家 网络编程 m.aspxhome.com