Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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

Closed
wants to merge 2 commits into from

3 participants

Dave McLain Tim Graham Marc Tamlyn
Dave McLain

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))
40 40
 instance that is used to service a request. Using the previous example,
41 41
 this means that every request on ``MyView`` is able to use ``self.size``.
  42
+Arguments must correspond to attributes that can be set on the class using
  43
+``setattr``.
3
Marc Tamlyn Owner

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

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?

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
Tim Graham
Owner

I've merged this, thanks!

Tim Graham timgraham closed this November 22, 2012
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.
5  django/views/generic/base.py
@@ -53,8 +53,9 @@ def as_view(cls, **initkwargs):
53 53
                                 "keyword argument to %s(). Don't do that."
54 54
                                 % (key, cls.__name__))
55 55
             if not hasattr(cls, key):
56  
-                raise TypeError("%s() received an invalid keyword %r" % (
57  
-                    cls.__name__, key))
  56
+                raise TypeError("%s() received an invalid keyword %r. as_view only "
  57
+                        "accepts arguments that are already attributes of the class"
  58
+                        % (cls.__name__, key))
58 59
 
59 60
         def view(request, *args, **kwargs):
60 61
             self = cls(**initkwargs)
4  docs/ref/class-based-views/index.txt
@@ -36,9 +36,11 @@ A class-based view is deployed into a URL pattern using the
36 36
     is modified, the actions of one user visiting your view could have an
37 37
     effect on subsequent users visiting the same view.
38 38
 
39  
-Any argument passed into :meth:`~View.as_view()` will be assigned onto the
  39
+Arguments passed into :meth:`~View.as_view()` will be assigned onto the
40 40
 instance that is used to service a request. Using the previous example,
41 41
 this means that every request on ``MyView`` is able to use ``self.size``.
  42
+Arguments must correspond to attributes that return True on a ``hasattr`` 
  43
+check.
42 44
 
43 45
 Base vs Generic views
44 46
 ---------------------
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.