Django 请求对象起源于 `class View` 在哪里?

Where does Django request object originated in `class View`?

下面是我的代码段referring to

@classonlymethod
def as_view(cls, **initkwargs):
    """
    Main entry point for a request-response process.
    """
    for key in initkwargs:
        if key in cls.http_method_names:
            raise TypeError("You tried to pass in the %s method name as a "
                            "keyword argument to %s(). Don't do that."
                            % (key, cls.__name__))
        if not hasattr(cls, key):
            raise TypeError("%s() received an invalid keyword %r. as_view "
                            "only accepts arguments that are already "
                            "attributes of the class." % (cls.__name__, key))

    def view(request, *args, **kwargs):
        self = cls(**initkwargs)
        if hasattr(self, 'get') and not hasattr(self, 'head'):
            self.head = self.get
        self.request = request
        self.args = args
        self.kwargs = kwargs
        return self.dispatch(request, *args, **kwargs)
    view.view_class = cls
    view.view_initkwargs = initkwargs

    # take name and docstring from class
    update_wrapper(view, cls, updated=())

    # and possible attributes set by decorators
    # like csrf_exempt from dispatch
    update_wrapper(view, cls.dispatch, assigned=())
    return view

我在寻找请求对象传入的代码。

常用as_view的地方在url

但是我无法在

中引用请求对象
def url(regex, view, kwargs=None, name=None, prefix=''):
    if isinstance(view, (list, tuple)):
        # For include(...) processing.
        urlconf_module, app_name, namespace = view
        return RegexURLResolver(regex, urlconf_module, kwargs, app_name=app_name, namespace=namespace)
    else:
        if isinstance(view, six.string_types):
            warnings.warn(
                'Support for string view arguments to url() is deprecated and '
                'will be removed in Django 1.10 (got %s). Pass the callable '
               'instead.' % view,
            RemovedInDjango110Warning, stacklevel=2
            )
            if not view:
                raise ImproperlyConfigured('Empty URL pattern view name not permitted (for pattern %r)' % regex)
            if prefix:
                view = prefix + '.' + view
        return RegexURLPattern(regex, view, kwargs, name)

谁能给我指明方向?

请求由BaseHandler.get_response处理:

wrapped_callback = self.make_view_atomic(callback)
try:
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
    ...

请求是从 WSGIHandler class 创建的。

James Bennett 在 Django In Depth at around 2 hours and 14 minutes. Slides can be found here.

中谈到了这个

请注意,请求永远不会传递给 as_view()

在加载 url 配置时调用 as_view() 方法,然后再处理任何请求。它定义了一个方法view,returns它。

def view(request, *args, **kwargs):
    ...
return view

view 方法采用参数 request、位置参数和关键字参数。然后 view 方法被传递给 url 实例。请注意,url 只需要一个带有 request 参数的可调用对象。这可能是通过为基于 class 的视图或基于常规函数的视图调用 as_view() 返回的可调用对象,这对将请求传递到视图的方式没有影响。

def function_view(request, *args, **kwargs):
    return HttpResponse("I'm a function based view")

url(r'^cbv/$', MyView.as_view()),
url(r'^fv/$', function_view), 

然后,当请求被处理时,url 被解析为这个 view,并且 BaseHandler.get_response 调用带有请求的视图,并从 url.