欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django REST框架創(chuàng)建一個(gè)簡(jiǎn)單的Api實(shí)例講解

 更新時(shí)間:2019年11月05日 14:31:06   作者:太陽(yáng)以西  
在本篇文章里小編給大家整理的是關(guān)于Django REST框架創(chuàng)建一個(gè)簡(jiǎn)單的Api實(shí)例講解,有需要的朋友們可以學(xué)習(xí)下。

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)文章

  • pytorch中的 .view()函數(shù)的用法介紹

    pytorch中的 .view()函數(shù)的用法介紹

    這篇文章主要介紹了pytorch中的 .view()函數(shù)的用法,主要介紹兩種方法手動(dòng)調(diào)整size和自動(dòng)調(diào)整size,下面具體方法分析需要的小伙伴可以參考一下
    2022-03-03
  • 詳解Pandas的三大利器(map,apply,applymap)

    詳解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í)例代碼

    這篇文章主要介紹了三個(gè)python爬蟲(chóng)項(xiàng)目實(shí)例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Python?使用pip在windows命令行中安裝HDF?reader包的操作方法

    Python?使用pip在windows命令行中安裝HDF?reader包的操作方法

    HDF reader包是一個(gè)常用來(lái)將.mat類型數(shù)據(jù)導(dǎo)入到python在這里插入代碼片中使用的包,非常好用,今天介紹一下,如何在命令行中安裝這個(gè)包,需要的朋友可以參考下
    2022-12-12
  • python程序如何進(jìn)行保存

    python程序如何進(jìn)行保存

    在本篇文章里小編給大家整理了關(guān)于python程序保存的方法,有需要的朋友們可以學(xué)習(xí)下。
    2020-07-07
  • 利用Python的pandas數(shù)據(jù)處理包將寬表變成窄表

    利用Python的pandas數(shù)據(jù)處理包將寬表變成窄表

    這篇文章主要介紹了利用Python的pandas數(shù)據(jù)處理包將寬表變成窄表,文章通過(guò)圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • Python實(shí)現(xiàn)的HMacMD5加密算法示例

    Python實(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-04
  • Python實(shí)現(xiàn)企業(yè)微信機(jī)器人每天定時(shí)發(fā)消息實(shí)例

    Python實(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-02
  • Python socket模塊實(shí)現(xiàn)的udp通信功能示例

    Python 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
  • python使用PyQt5的簡(jiǎn)單方法

    python使用PyQt5的簡(jiǎn)單方法

    這篇文章主要介紹了python使用PyQt5的簡(jiǎn)單方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02

最新評(píng)論