Django REST框架創(chuàng)建一個(gè)簡(jiǎn)單的Api實(shí)例講解
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
以上就是本次介紹的關(guān)于Django REST框架創(chuàng)建一個(gè)簡(jiǎn)單的Api實(shí)例講解內(nèi)容,感謝大家的學(xué)習(xí)和對(duì)腳本之家的支持。
相關(guān)文章
詳解Pandas的三大利器(map,apply,applymap)
這篇文章主要為大家介紹了pandas中的三大利器: map、apply、applymap,他們經(jīng)常在進(jìn)行數(shù)據(jù)處理的時(shí)候用到,需要的可以參考一下2022-02-02三個(gè)python爬蟲(chóng)項(xiàng)目實(shí)例代碼
這篇文章主要介紹了三個(gè)python爬蟲(chóng)項(xiàng)目實(shí)例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Python?使用pip在windows命令行中安裝HDF?reader包的操作方法
HDF reader包是一個(gè)常用來(lái)將.mat類型數(shù)據(jù)導(dǎo)入到python在這里插入代碼片中使用的包,非常好用,今天介紹一下,如何在命令行中安裝這個(gè)包,需要的朋友可以參考下2022-12-12利用Python的pandas數(shù)據(jù)處理包將寬表變成窄表
這篇文章主要介紹了利用Python的pandas數(shù)據(jù)處理包將寬表變成窄表,文章通過(guò)圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09Python實(shí)現(xiàn)的HMacMD5加密算法示例
這篇文章主要介紹了Python實(shí)現(xiàn)的HMacMD5加密算法,簡(jiǎn)單說(shuō)明了HMAC-MD5加密算法的概念、原理并結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)HMAC-MD5加密算法的相關(guān)操作技巧,,末尾還附帶了Java實(shí)現(xiàn)HMAC-MD5加密算法的示例,需要的朋友可以參考下2018-04-04Python實(shí)現(xiàn)企業(yè)微信機(jī)器人每天定時(shí)發(fā)消息實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)企業(yè)微信機(jī)器人每天定時(shí)發(fā)消息實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Python socket模塊實(shí)現(xiàn)的udp通信功能示例
這篇文章主要介紹了Python socket模塊實(shí)現(xiàn)的udp通信功能,結(jié)合具體實(shí)例形式分析了Python使用socket模塊實(shí)現(xiàn)UDP通信客戶端與服務(wù)器端相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-04-04