前言

django api接口 有两种模式,一个是FBV,另一个是CBV。首要推荐的是CBV 模式。

FBV 模式的API 接口

使用函数,通过if/else 语句根据请求方法进行数据返回/接口。

from django.shortcuts import render,HttpResponse
import json
from django.views.decorators.csrf import csrf_exempt

# Create your views here.


@csrf_exempt                   #对此试图函数添加csrf装饰器,使得此函数的post请求免验证tooken
def Asset(request):
    if request.method == "POST":
        info = json.loads(request.body.decode('utf-8'))
        print(info)
        return HttpResponse('收到了')
    else:
        host_list = ['c1.com', 'c2.com', 'c3.com']
        return HttpResponse(json.dumps(host_list))
View Code

相关文章:

  • 2021-09-12
  • 2021-11-23
  • 2022-12-23
  • 2022-02-02
  • 2022-01-25
  • 2022-02-23
猜你喜欢
  • 2021-10-11
  • 2022-12-23
  • 2021-06-08
  • 2021-06-15
  • 2022-03-09
  • 2021-09-19
  • 2021-11-18
相关资源
相似解决方案