最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

  • Flask中提供靜態(tài)文件的實(shí)例講解

    Flask中提供靜態(tài)文件的實(shí)例講解

    在本篇文章里小編給大家分享的是一篇關(guān)于Flask中提供靜態(tài)文件的實(shí)例及相關(guān)知識(shí)點(diǎn)詳解,有興趣的朋友們可以跟著學(xué)習(xí)下。
    2021-12-12
  • Python實(shí)現(xiàn)的遠(yuǎn)程登錄windows系統(tǒng)功能示例

    Python實(shí)現(xiàn)的遠(yuǎn)程登錄windows系統(tǒng)功能示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的遠(yuǎn)程登錄windows系統(tǒng)功能,結(jié)合實(shí)例形式分析了Python基于wmi模塊的遠(yuǎn)程連接與進(jìn)程操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-06-06
  • Python 正則表達(dá)式的高級(jí)用法

    Python 正則表達(dá)式的高級(jí)用法

    作為一個(gè)概念而言,正則表達(dá)式對(duì)于Python來(lái)說(shuō)并不是獨(dú)有的。但是,Python中的正則表達(dá)式在實(shí)際使用過(guò)程中還是有一些細(xì)小的差別。本文是一系列關(guān)于Python正則表達(dá)式文章的其中一部分。
    2016-12-12
  • 淺析python的優(yōu)勢(shì)和不足之處

    淺析python的優(yōu)勢(shì)和不足之處

    在本篇內(nèi)容中小編給大家整理了關(guān)于分析python的優(yōu)勢(shì)和不足的分析,有需要的朋友們參考下。
    2018-11-11
  • Python機(jī)器學(xué)習(xí)pytorch模型選擇及欠擬合和過(guò)擬合詳解

    Python機(jī)器學(xué)習(xí)pytorch模型選擇及欠擬合和過(guò)擬合詳解

    如何發(fā)現(xiàn)可以泛化的模式是機(jī)器學(xué)習(xí)的根本問(wèn)題,將模型在訓(xùn)練數(shù)據(jù)上過(guò)擬合得比潛在分布中更接近的現(xiàn)象稱為過(guò)擬合,用于對(duì)抗過(guò)擬合的技術(shù)稱為正則化
    2021-10-10
  • Windows下Python3.6安裝第三方模塊的方法

    Windows下Python3.6安裝第三方模塊的方法

    這篇文章主要介紹了Windows下Python3.6安裝第三方模塊的方法,需要的朋友可以參考下
    2018-11-11
  • Python?scipy利用快速傅里葉變換實(shí)現(xiàn)濾波

    Python?scipy利用快速傅里葉變換實(shí)現(xiàn)濾波

    這篇文章主要為大家詳細(xì)介紹了Python?scipy如何利用快速傅里葉變換實(shí)現(xiàn)濾波,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • Python tkinter 下拉日歷控件代碼

    Python tkinter 下拉日歷控件代碼

    這篇文章主要介紹了Python tkinter 下拉日歷控件代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • 詳解如何使用Pandas刪除DataFrame中的非數(shù)字類型數(shù)據(jù)

    詳解如何使用Pandas刪除DataFrame中的非數(shù)字類型數(shù)據(jù)

    在數(shù)據(jù)處理和分析過(guò)程中,經(jīng)常會(huì)遇到需要清洗數(shù)據(jù)的情況,本文將詳細(xì)介紹如何使用Pandas刪除DataFrame中的非數(shù)字類型數(shù)據(jù),感興趣的小伙伴可以了解下
    2024-03-03
  • Python網(wǎng)絡(luò)爬蟲技術(shù)高階用法

    Python網(wǎng)絡(luò)爬蟲技術(shù)高階用法

    網(wǎng)絡(luò)爬蟲成為了自動(dòng)化數(shù)據(jù)抓取的核心工具,Python?擁有強(qiáng)大的第三方庫(kù)支持,在網(wǎng)絡(luò)爬蟲領(lǐng)域的應(yīng)用尤為廣泛,本文將深入探討?Python?網(wǎng)絡(luò)爬蟲的高階用法,包括處理反爬蟲機(jī)制、動(dòng)態(tài)網(wǎng)頁(yè)抓取、分布式爬蟲以及并發(fā)和異步爬蟲等技術(shù),幫助讀者掌握高級(jí)Python爬蟲技術(shù)
    2024-12-12

最新評(píng)論

曲沃县| 贵南县| 克什克腾旗| 东宁县| 万安县| 安西县| 黑山县| 禹州市| 运城市| 新蔡县| 青铜峡市| 绥江县| 庄河市| 沈丘县| 邹平县| 波密县| 广河县| 庆安县| 枝江市| 天长市| 乌审旗| 攀枝花市| 达孜县| 苍梧县| 秭归县| 阜平县| 建水县| 长岛县| 手游| 独山县| 南和县| 浦北县| 巴楚县| 西宁市| 临安市| 大竹县| 河津市| 松江区| 曲麻莱县| 萍乡市| 玛沁县|