Django 通过拼接路由,获取请求接口中的form-data形式参数
代码部分
提示:这里可以添加本文要记录的大概内容:
主项目路由写法:
主目录下urls.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# API_urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^api/xxx/v2/',include((API_urls,'Other_Api'),namespace='Other_Api')),
# 单起一个完整应用的目录
# url(r'^upgrade/index$',views.,name='update'),
]
主目录同级目录下 urls与view是的写法
Other_Api目录下的urls与views
Other_Api文件夹与主目录文件夹同级别,并且该文件夹下存放了其他不同的项目
Other_Api/API_urls.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# API_urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^upgrade/index$',views.UPGRADE.as_view(),name='update'),
# url(r'^upgrade/index$',views.,name='update'),
]
Other_Api/views.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
from Other_Api.upgrade import views
'''
新添加添加升级
'''
class UPGRADE(APIView):
def post(self, request):
print("~~~~~~~~~~~~",request)
# ~~~~~~~~~~~~ <rest_framework.request.Request object at 0x10dd2e1c0>
# print(request.body) #请求的主体,返回的是一个字符串
# 请求的数据部分,返回的是一个字典对象
print("00000000000000",request.data) # 作用与request.POST是一样的
# 00000000000000 <QueryDict: {'version': ['8.0.0']}>
print("6666666666666", json.dumps(request.data))
# 6666666666666 {"version": "8.0.0"}
print("7777777777",request.POST)
# 7777777777 <QueryDict: {'version': ['8.0.0']}>
# 按照百度说的编写,这两个无法使用
# print("22222222222222", request.argument)
# print("11111111111111", request.body_arguments)
try:
# 传递接口中的请求参数
# requestbody = json.loads(request.data) # 获取的是请求中原生的body{}
requestbody = json.dumps(request.data) # 获取的是postman中的form-data形式
except Exception as e:
execution_result = JsonResponseEntity([], status_code.HTTP_500_INTERNAL_SERVER_ERROR, '请求格式错误')
return api_response.JsonResponse(execution_result)
results = views.test_get_post(requestbody)
print(results)
# {'version': '8.0.0'}
return results
# def get(self,request):
# print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~22222222222")
# execution_results = views.test_get(request)
# return api_response.JsonResponse(execution_results)
class GET_STUDENT(APIView):
def get(self,request):
requestbody = json.loads(json.dumps(request.GET))
if requestbody["uid"] in ['',None] and requestbody["card"] in ['',None] and requestbody["stuname"] in ['',None]:
execution_results = JsonResponseEntity([],status_code.HTTP_500_INTERNAL_SERVER_ERROR,'学生uid、学生姓名、学生card至少一个不为空')
return api_response.JsonResponse(execution_results)
execution_results = query_studentinfo(request)
return api_response.JsonResponse(execution_results)
class GET_LIVE(APIView):
def get(self,request):
execution_results = query_liveinfo(request)
return api_response.JsonResponse(execution_results)
Other_Api目录下的upgrade文件夹下的写法
upgrade/views.py
import json, logging
import requests
from django.http import HttpResponse, HttpResponseRedirect
# 尝试百度说的使用encode,似乎在我这不起作用
# from urllib3 import encode_multipart_formdata
# Create your views here.
# 登陆网页获取请求数据
host = {"test":"https://test-xxx.vdyoo.com/upgrade-server", "online":"https://lc-xxx/upgrade-server"}
choose = host['test']
logger = logging.getLogger('django')
def test_get_post(request):
print(type(json.loads(request))) # <class 'dict'>
re = json.loads(request) # 以字典的形式获取请求接口中form-data中的请求参数
print(re.get("version")) # 获取字典中version的值
return HttpResponse('test request ok')
# if request.method == 'POST':
# print("------------------------------")
# print('method is', request.method)
# print('querystring is', request.POST)
# print(request.POST.getlist())
# version = request.POST.get('version')
# version = request.POST['version']
# version = request.formdata
# print("vvvvvvvvvvvv~~~~~~~", version)
def test_get(request):
print('path info is', request.path_info)
print('method is', request.method)
print('querystring is', request.GET)
return HttpResponse('test request ok')