您好,登錄后才能下訂單哦!
這篇文章主要介紹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”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。