Django 中如何 request.method == None

发布于 2024-12-11 04:11:55 字数 279 浏览 0 评论 0原文

我试图从 https 找出与 Django 相关的问题://github.com/miki725/Django-jQuery-File-Uploader-Integration-demo/issues/1

在什么条件下Django中可以request.method == None

I am trying to figure out this issue related to Django from https://github.com/miki725/Django-jQuery-File-Uploader-Integration-demo/issues/1

Under what conditions can request.method == None in Django?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨小沫ゞ 2024-12-18 04:11:55

TL;DR: request.method 在实际使用中永远不会 None ,但对于您的特定情况,您正在寻找错误的东西。

通用HttpRequest

django/http/__init__.py

class HttpRequest(object):
    ...
    def __init__(self):
        ...
        self.method = None
        ...

当实例化普通HttpRequest时,其方法为None。但是,WSGIRequestModPythonRequest 永远不会调用 HttpRequest.__init__

通过 mod_wsgi 请求

django/core/handlers/wsgi.py

class WSGIRequest(http.HttpRequest):
    ...
    def __init__(self, environ):
        ...
        self.method = environ['REQUEST_METHOD'].upper()
        ...

总结来说,对于 mod_wsgi,request.method永远不会。如果您以某种扭曲的方式设法使 environ['REQUEST_METHOD'] 未定义或为 None,则请求将失败。

通过 mod_python 的请求

django/core/handlers/modpython.py

class ModPythonRequest(http.HttpRequest):
    ...
    def _get_method(self):
        return self.META['REQUEST_METHOD'].upper()
    ...
    method = property(_get_method)

WSGIRequest 的注释相同。永远不可能是None

测试客户端

django.test.client.RequestFactory.request 实例化一个 WSGIRequest 并每次使用 environ< 中定义的 REQUEST_METHOD 进行调用/code> 作为大写字符串,正如它应该的那样。


摘要:

assert request.method is not None

您在错误的位置查找错误。在本例中,request.method == 'POST'。当 request.META.get('CONTENT_TYPE', '') 为 None 时失败。原因是客户端没有在请求中发送 Content-Type 标头(不要问我为什么,我对这些东西不熟悉)。

TL;DR: request.method is never None in real use, but for your particular case, you're looking at the wrong thing.

Generic HttpRequest

django/http/__init__.py:

class HttpRequest(object):
    ...
    def __init__(self):
        ...
        self.method = None
        ...

When a plain HttpRequest is instantiated, its method is None. But then, WSGIRequest and ModPythonRequest don't call HttpRequest.__init__ ever.

Requests through mod_wsgi

django/core/handlers/wsgi.py:

class WSGIRequest(http.HttpRequest):
    ...
    def __init__(self, environ):
        ...
        self.method = environ['REQUEST_METHOD'].upper()
        ...

The summary of this is that for mod_wsgi, request.method will never be None. If in some warped way you managed to get environ['REQUEST_METHOD'] not being defined or being None, the request would fail.

Requests through mod_python

django/core/handlers/modpython.py:

class ModPythonRequest(http.HttpRequest):
    ...
    def _get_method(self):
        return self.META['REQUEST_METHOD'].upper()
    ...
    method = property(_get_method)

Same remarks as with WSGIRequest apply. Can't ever be None.

Test client

django.test.client.RequestFactory.request instantiates a WSGIRequest and is called every time with REQUEST_METHOD defined in the environ as an uppercase string, as it should be.


Summary:

assert request.method is not None

You're looking for the error in the wrong place. In this case, request.method == 'POST'. It's failing when request.META.get('CONTENT_TYPE', '') is None. The reason for this is that the Content-Type header isn't being sent by the client in the request (don't ask me why, I'm not familiar with that stuff).

情栀口红 2024-12-18 04:11:55

我能够通过 AJAX 实现这一点,特别是调用 jQuery 的 $.ajax 函数(请参阅 http:// api.jquery.com/jQuery.ajax/) 具有“POST”类型但没有数据:

    $.ajax({
      type: "POST",
      url: "/something",
    });

I was able to get this to happen via AJAX, specifically calling jQuery's $.ajax function (see http://api.jquery.com/jQuery.ajax/) with "POST" type but no data:

    $.ajax({
      type: "POST",
      url: "/something",
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文