簡體   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