django: how do I migrate this view to class-based generic view equivalent?
I have trouble transforming the following code into the new django 开发者_如何学运维1.3 class-based generic view format. Specifically, I don't understand how I can pass the 'extra_context' to the class based view. Can someone help me transform this code to the new notation? or post a link to a good example? I've read the docs but the example is very flimsy.
def return_event_list_object(request, username, queryset, entries_per_page, param1, param2):
...
...
return object_list(request, queryset = queryset,
template_name = 'myapp/list_events.html',
paginate_by = int(entries_per_page),
template_object_name = 'event',
extra_context = {'param1': param1,
'param2': param2, } )
I appreciate your input!
The extra_context
section of the docs explains how to add items to the context:
Class-based views don't provide an extra_context argument. Instead, you subclass the view, overriding get_context_data(). For example:
In your case, try:
class MyListView(ListView):
def get_context_data(self, **kwargs):
context = super(MyListView, self).get_context_data(**kwargs)
context.update({
'param1': kwargs['param2'],
'param2': kwargs['param1']
})
return context
精彩评论