时间:2021-05-22
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 jangoconda install -c conda-forge djangorestframeworkStep 1
Create the django project, open the command prompt therre and enter the following command:
django-admin startproject SampleProjectStep 2
Navigate the project folder and create a web app using the command line.
python manage.py startapp MyAppStep 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 renderfrom django.http import Http404from rest_framework.views import APIViewfrom rest_framework.decorators import api_viewfrom rest_framework.response import Responsefrom rest_framework import statusfrom django.http import JsonResponsefrom django.core import serializersfrom django.conf import settingsimport 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 urlfrom django.contrib import adminfrom MyApp import viewsurlpatterns = [ 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 runserverFinally open the url:
http://127.0.0.1:8000/idealweight/
References:
Create a Simple API Using Django REST Framework in Python
以上就是本次介绍的关于Django REST框架创建一个简单的Api实例讲解内容,感谢大家的学习和对的支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
在Django中用rest_framework写API,写了一个用户注册的API,并测试成功。本人环境:Django==2.2.1;djangorestfram
我使用django-rest-framework开发了一个API.我正在使用ModelSerializer返回模型的数据.models.pyclassMetaT
在django的views中不论是用类方式还是用装饰器方式来使用rest框架,django_rest_frame实现权限管理都需要两个东西的配合:authent
问题你想使用一个简单的REST接口通过网络远程控制或访问你的应用程序,但是你又不想自己去安装一个完整的web框架。解决方案构建一个REST风格的接口最简单的方法
本文实例讲述了Python实现手写一个类似django的web框架。分享给大家供大家参考,具体如下:用与django相似结构写一个web框架。启动文件代码:fr