With regular views, RequestContext
variables can be accessed just like request.VARNAME
:
def example(request, template_name='stuff_list'):
return render_to_response(template_name,
{'stuff_list': get_list_or_404(Stuff, foo=request.DEBUG)},
context_instance=RequestContext(request))
... instead of setting context_instance
I could call function-based generic view direct_to_template
1
How do I read variables added to RequestContext inside class-based generic views 2?
For example:
class ArticleListView(ListView):
template_name = 'stuff_list'
bar = request.DEBUG # This won't work. What should I use instead?
queryset = get_list_or_404(Stuff, foo=bar)
TemplateView
anyway.You need to use a callback — get_queryset()
in this case — instead of the class attributes. Class attributes are really just shortcuts when you're controlling options statically, and they're limited to some pretty simple things. When you need to do something more complex, you'll want to switch to a callback instead.
In your case, code like the following should work:
class ArticleListView(ListView):
template_name = 'stuff_list'
def get_queryset(self):
return get_list_or_404(Stuff, foo=self.request.DEBUG)
For more details, see the documentation.
RequestContext parameters are also regular context variables. You should be able to do just {{VARNAME}}
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