Reputation: 2277
Here is the code segment I am 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
I am looking for the code that the request object is passed in to.
The common place where as_view is used is in url
However I couldn't reference to request object in
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)
Can someone point me a direction?
Upvotes: 1
Views: 521
Reputation: 309039
Note that the request is never passed to as_view()
.
The as_view()
method is called when the url config is loaded, before any requests are handled. It defines a method view
, and returns it.
def view(request, *args, **kwargs):
...
return view
This view
method takes an argument request
, positional and keyword arguments. The view
method is then passed to the url instance. Note that url
simply requires a callable that takes a request
argument. This could be a callable returned by calling as_view()
for a class based view, or a regular function based view, it makes no difference to how the request is passed to the 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),
Then, when a request is handled, the url is resolved into this view
, and BaseHandler.get_response
calls the view with the request, and args and kwargs captured from the url.
Upvotes: 3
Reputation: 10719
The request is created from the WSGIHandler class.
James Bennett talks about this in Django In Depth at around 2 hours and 14 minutes. Slides can be found here.
Upvotes: 1
Reputation: 298432
The request is processed by BaseHandler.get_response
:
wrapped_callback = self.make_view_atomic(callback)
try:
response = wrapped_callback(request, *callback_args, **callback_kwargs)
...
Upvotes: 1