Django REST框架创建一个简单的Api实例讲解
作者:太阳以西 发布时间:2023-04-28 01:02:40
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
来源:https://www.cnblogs.com/zhichun/p/11797759.html
猜你喜欢
- 自定义可迭代的类列表可以获取列表的长度,然后使用变量i对列表索引进行循环,也可以获取集合的所有元素,且容易理解。没错,使用列表的代码是容易理
- 目录前言第一步:首先安装相关的依赖包第二步:在django项目配置文件settings.py中注册应用第三步:在django项目配置文件se
- 在编程时你一定碰到过时间触发的事件,在VB中有timer控件,而asp中没有, 假如你要不停地查询数据库来等待一个返回结果的话,我想你一定知
- 本文实例为大家分享了OpenCV实现直线检测的具体代码,供大家参考,具体内容如下1 介绍本文主要介绍OpenCV自带的直线检测函数Hough
- 1. desc 命令 格式: desc tablename columnname 例子: desc `table` `mid` desc `
- 一直以来,每次调用Ajax方法都需要创建一次 Microsoft.XMLHTTP 对象,今天在使用Ajax技术做一个类似聊天室的
- 本文实例为大家分享了python保存网页图片到本地的具体代码,供大家参考,具体内容如下#!/usr/bin/env Python#codin
- 一. 安装 Beautiful Soup首先,您需要安装 Beautiful Soup。在终端或命令提示符中运行以下命令:pip insta
- 1、yield,将函数变为 generator (生成器)例如:斐波那契数列def fib(num): a, b, c = 1,
- 问题描述vscode中跨目录的模块调用远不如pycharm中的来的简单,在pycharm中即使是不同库文件夹中子函数也可以进行互相调用。而在
- 开发中偶尔需要判断网络的连通性,没有什么方法比 ping 更直接了当,通常检查网络情况都是运行命令ping www.baidu.com ,查
- 本篇文章主要内容代理类主要功能是将一个类实例的属性访问和控制代理到代码内部另外一个实例类,将想对外公布的属性的访问和控制权交给代理类来操作,
- 1、保存整个网络结构信息和模型参数信息:torch.save(model_object, './model.pth')直接加
- 本文实例讲述了python统计字符串中指定字符出现次数的方法。分享给大家供大家参考。具体如下:python统计字符串中指定字符出现的次数,例
- 内容摘要:为什么要什么XML文件:其优势就是处理该XML数据的文档可以是静态文档,比如HTML文件通过Javascript、XMLDOM来解
- 本文实例讲述了Python实现的生产者、消费者问题。分享给大家供大家参考,具体如下:生产者、消费者问题,经典的线程同步问题:假设有一个缓冲池
- 前言 本篇文章介绍一下 Pycharm 如何配置远程连接信息,使其能够在本地使用服务器上的GPU等硬件资源,并在本地完成代码的运行与调试。
- 这篇文章主要介绍了Python语言异常处理测试过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 通常在多个不等式的时候,需要分着写,比如x = 1if x>0 and x<3: print(True)但是在Python中居然
- 网页兼容测试,除了做不同浏览器的兼容测试,还要观察网页在不同分辨率下的表现情况。在页面中使用了CSS绝对定位,发现在宽屏下错位。随后测试非1