From 7b2d95eb3054e95d2fa4c36d8194cd74816118a7 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 22 Nov 2012 09:07:21 -0500 Subject: [PATCH] Clarified usage of as_view kwargs for setting arguments on class based views Thanks Dave McLain for the patch. --- django/views/generic/base.py | 5 +++-- docs/ref/class-based-views/index.txt | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/django/views/generic/base.py b/django/views/generic/base.py index 23e18c54a02..d0045a2ef09 100644 --- a/django/views/generic/base.py +++ b/django/views/generic/base.py @@ -54,8 +54,9 @@ class View(object): "keyword argument to %s(). Don't do that." % (key, cls.__name__)) if not hasattr(cls, key): - raise TypeError("%s() received an invalid keyword %r" % ( - cls.__name__, 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) diff --git a/docs/ref/class-based-views/index.txt b/docs/ref/class-based-views/index.txt index c4b632604a4..a0279534166 100644 --- a/docs/ref/class-based-views/index.txt +++ b/docs/ref/class-based-views/index.txt @@ -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 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 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 ---------------------