如何用Django处理HTTP.Request,请求内容类型,查询参数

How to handle with Django HTTP.Request, request content-type, query parameters

大家好,我是 Python 的新手,Django 实际上也在编码领域。

我想构建一个应用程序,它可以通过 Content_Type 'application/xml'.

接收 POST-请求

我不明白如何处理 django 中的 HTTP.Request.META。 首先我想检查 Content_type,然后是 Query_string,然后是 Content_Lenght。

from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render
from django.http import (
HttpResponse, HttpResponseNotAllowed, HttpRequest,)


@csrf_exempt
# Check the HTTP Request Method
def app(request):
    if request.method != "POST":
        return HttpResponseNotAllowed(permitted_methods=('POST',))
    else:
       # checkcontent(request)
    return HttpResponse('OK')

“““
def checkcontent(request):
    if not 'application/xml' in request.meta['CONTENT_TYPE']:
        raise Exception("Invalid content-type. The expected request content-type is 'application/xml'")

“““

评论区不起作用!

谁能给我解释一下?

感谢

首先,django中的http请求here are all available headers

所以你需要:

request.META['CONTENT_TYPE']

而不是

request.meta['CONTENT_TYPE']