Clarified usage of as_view kwargs for setting arguments on class based views

Thanks Dave McLain for the patch.
This commit is contained in:
Tim Graham 2012-11-22 09:07:21 -05:00
parent 3587991ba8
commit 7b2d95eb30
2 changed files with 6 additions and 4 deletions

View File

@ -54,8 +54,9 @@ class View(object):
"keyword argument to %s(). Don't do that." "keyword argument to %s(). Don't do that."
% (key, cls.__name__)) % (key, cls.__name__))
if not hasattr(cls, key): if not hasattr(cls, key):
raise TypeError("%s() received an invalid keyword %r" % ( raise TypeError("%s() received an invalid keyword %r. as_view "
cls.__name__, key)) "only accepts arguments that are already "
"attributes of the class." % (cls.__name__, key))
def view(request, *args, **kwargs): def view(request, *args, **kwargs):
self = cls(**initkwargs) self = cls(**initkwargs)

View File

@ -37,10 +37,11 @@ A class-based view is deployed into a URL pattern using the
is modified, the actions of one user visiting your view could have an is modified, the actions of one user visiting your view could have an
effect on subsequent users visiting the same view. effect on subsequent users visiting the same view.
Any argument passed into :meth:`~django.views.generic.base.View.as_view()` will Arguments passed into :meth:`~django.views.generic.base.View.as_view()` will
be assigned onto the instance that is used to service a request. Using the be assigned onto the instance that is used to service a request. Using the
previous example, this means that every request on ``MyView`` is able to use previous example, this means that every request on ``MyView`` is able to use
``self.size``. ``self.size``. Arguments must correspond to attributes that already exist on
the class (return ``True`` on a ``hasattr`` check).
Base vs Generic views Base vs Generic views
--------------------- ---------------------