django - How to read variables added to RequestContext inside class-based generic views? -
with regular views, requestcontext variables can accessed 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 call function-based generic view direct_to_template1
how read variables added requestcontext inside class-based generic views 2?
for example:
class articlelistview(listview): template_name = 'stuff_list' bar = request.debug # won't work. should use instead? queryset = get_list_or_404(stuff, foo=bar) 1 replaced class-based
templateview anyway.2 new in django 1.3 , want use them because.
you need use callback — get_queryset() in case — instead of class attributes. class attributes shortcuts when you're controlling options statically, , they're limited pretty simple things. when need more complex, you'll want switch callback instead.
in case, code 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 documentation.
Comments
Post a Comment