This is my view (simplified):
class MyView(TemplateView):
def __init__(self):
self.foo = 'bar'
super(MyView, self).__init__()
This is in urls.py:
url(
r'^/foo/$',
MyView.as_view(foo='baz'), name='my_view'
)
When I run this, I get the following error:
TypeError: MyView() received an invalid keyword 'foo'. as_view only accepts arguments that are already attributes of the class.
Why? I thought this would work. :/
At least according to this post: http://reinout.vanrees.org/weblog/2011/08/24/class-based-views-walkthrough.html#class-view
If I understood this correctly, this should have set the attribute foo
to the value 'baz'
passed in as_view
. Without any attributes in as_view
, the value should be 'bar'
, as defined in __init__
.
Any arguments passed to as_view() will override attributes set on the class. In this example, we set template_name on the TemplateView . A similar overriding pattern can be used for the url attribute on RedirectView .
Django provides several class based generic views to accomplish common tasks. The simplest among them is TemplateView. It Renders a given template, with the context containing parameters captured in the URL. TemplateView should be used when you want to present some information on an HTML page.
self. request = request is set in view function that as_view() returns. I looked into the history, but only found setting self. request and then immediately passing request into the view function.
You're explicitly setting a value on an instance of the class in __init__()
. However, the class itself still doesn't have an attribute foo
, as it is unaware of dynamic attributes on instances: hasattr(MyView, 'foo')
always returns False
.
This would work as you expected your code to work:
class MyView(TemplateView):
foo = 'bar'
url(
r'^/foo/$',
MyView.as_view(foo='baz'), name='my_view'
)
The source code of Django View
class has this though:
if not hasattr(cls, key):
raise TypeError
So it is not enough to create the attr on an instance in __init__
...it has to exist on the class itself:
class MyView(TemplateView):
foo = 'bar'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With