Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Clarify usage of as_view kwargs for setting arguments on class based views #314

Closed
wants to merge 2 commits into from

3 participants

@dmclain

The current documentation could lead to a new Python developer thinking that passing a keyword argument into as_view will result in an attribute being specified and set to the correct value. The current error message doesn't provide any additional information that might help the user towards a solution.

docs/ref/class-based-views/index.txt
((6 lines not shown))
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``.
+Arguments must correspond to attributes that can be set on the class using
+``setattr``.
@mjtamlyn Collaborator
mjtamlyn added a note

I don't think this is right - you can setattr any argument on a view, but it has to be getattr-able first.

@dmclain
dmclain added a note

Technically, the attribue must be getattr-able first and then seattr-able afterwards. A read-only attribute would not be settable via passing an argument to as_view. I suppose it's most accurate to say that setting an attribute via a keyword argument to as_view will only work if hasattr(YourView, 'keyword_argument_name') returns True and that attribute is settable via setattr. getattr is not a sufficient condition because of the existence of read only properties. Can you have a Python object with a write-only attribute?

@dmclain
dmclain added a note

And apparently 2 years of developing Python still isn't enough to fully grok the way attributes work. You're right, getattr should be mentioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham
Owner

I've merged this, thanks!

@timgraham timgraham closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
5 django/views/generic/base.py
@@ -53,8 +53,9 @@ def as_view(cls, **initkwargs):
"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)
View
4 docs/ref/class-based-views/index.txt
@@ -36,9 +36,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:`~View.as_view()` will be assigned onto the
+Arguments passed into :meth:`~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``.
+Arguments must correspond to attributes that return True on a ``hasattr``
+check.
Base vs Generic views
---------------------
Something went wrong with that request. Please try again.