首页 > 技术文章 > 测试开发框django基础之写后端请求接口(二)

qd1228 2020-05-19 18:29 原文

该文章主要是介绍如何开发一个请求接口  请求接口的请求方式取决与前端传的类型,所以在定义api文档的时候,需要和前端沟通好,前端用什么格式给后端,后端接口接到请求后用什么类型格式返回给前端

1、如何处理一个请求,用户的请求不需要创建,只需要你使用

2、用户的响应需要你创建

requests如何用呢?

第一个方法:request.method 获取请求方式

如:

def hello(request):

  request.method

  if request.method =="GET":

    return render(request,template_name='mall.html',context={"name":"张三"})#context返回前端页面动态值

  else: request.method =="post":

    return render(request,template_name='error.html',context={"msg":"请求方法类错误"})

验证效果:http://127.0.0.1:8000/user/hello/  post请求返回error页面

安装一个插件:pip3 install djangorestframework==3.9.0  辅助开发后端接口

引用 from rest_framework.decorators import api_view

上面的代码修改为:

@api_view(["GET"])#代表支持get请求,post请求直接过滤掉了

def hello(request):

  request.method

  if request.method =="GET":

    return render(request,template_name='mall.html',context={"name":"张三"})#context返回前端页面动态值

def render(request, template_name, context=None, content_type=None, status=None, using=None):

  else: request.method =="post":

    return render(request,template_name='error.html',context={"msg":"请求方法类错误"})

验证效果,postman'请求post时,返回:

{
    "detail": "Method \"POST\" not allowed."
}

request的第二个方法:request.GET.get("url中参数的key")   #获取url地址栏获取参数

如:http://127.0.0.1:8000/mall/hello/?username=zhangsan&pwd=123

print(request.GEY)  打印结果是:<QueryDict: {'username': ['zhangsan'], 'pwd': ['123']}> 是个字典

字典取值两个方法:[key]或者用get()方法,一般用get(),    不建议使用这种取值request.GET['username']

@api_view(["GET","POST"])

def hello(request):

     #接口统一赋值

  usename = request.GET.get('username')
  pwd = request.GET.get("pwd")

#必填项做校验

   if username is not None and pwd is not None:

    return render(request, template_name='mall.html', context={"name": username}) # context返回前端页面动态值
  else:
    return render(request, template_name='error.html', context={"msg": "缺少必要得参数"})

验证效果:127.0.0.1:8000/mall/hello?username=zhangsan&pwd=123

第三个方法:获取url-encoding参数 通过postman 发送请求验证代码 request.POST

@api_view(["POST"])

def hello(request):

  usename = request.POST.get('username')
  pwd = request.POST.get("pwd")

   if username is not None and pwd is not None:

    return render(request, template_name='mall.html', context={"name": username}) # context返回前端页面动态值
  else:
    return render(request, template_name='error.html', context={"msg": "缺少必要得参数"})

验证效果:用postman  url写http://127.0.0.1:8000/user/hello/;body->url-encoding username=zhangsan passwoed=123

第四个方法:获取json串参数,通过postman发送请求验证,request.body;

json格式{"username':""} 取回来得是字符串 需要转化为字典 loads 请求时json数据 返回时页面

@api_view(["GET","POST"])

def hello(request):

  try:
    data = json.loads(request.body)#字符串转化未字典 字典转化如果错误呢,比如参数key传错,直接报500,如果报500说明开发没做处理,开发框架原生返回的 

  except:
    #return render(request, template_name='error.html', context={"msg": "缺少必要得参数"})

    return render(request, template_name='error.html', context={"msg": "请求格式非JSON格式"})
  username = data.get("username")
  pwd = data.get("pwd")
  if username is not None and pwd is not None:
    return render(request, template_name='mall.html', context={"name": username}) # context返回前端页面动态值
  else:
    return render(request, template_name='error.html', context={"msg": "缺少必要得参数"})

验证效果:用postman  url写http://127.0.0.1:8000/user/hello/;raw->json

{
"username":"zhangsan",
"password":"123"
}

第五个方法,上传文件 request.FILES   #通过postman验证,form-data 返回得时字典 二进制得数据

@api_view(["GET","POST"])
def hello(request):

  request.FILES.get("myfile")#获取上传文件名的key
  return HttpResponse(123)

验证效果:用postman  url写http://127.0.0.1:8000/user/hello/;form-data->file 

 

推荐阅读