繁体   English   中英

如何处理 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请求

所以你需要:

request.META['CONTENT_TYPE']

而不是

request.meta['CONTENT_TYPE']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM