I'm reading Two Scoops of Django: Best Practices for Django 1.8 where it is suggested to always use generic class based views.
So I'm trying to convert a page where I display a list of objects (def objects_listing(request)
) into a class derived from ListView (class ObjectsList(ListView)
).
The problem is that my view not only is needed to display a list of objects but also:
- A search form to filter these objects (which links on the same view);
- A list of categories of objects.
The solution appears to be to override the function get_context_data
to provide additional context to the template (see documentation).
My question is: is it still considered best practice to use ListView in this case? Or should I stick to a normal view instead? Which are the pros and cons of these approaches?
Pros of ListView
:
- The paginator works automatically in the ListView if you provide a
page
argument with GET. No need to pass it to the paginator object and to check for crazy user input: non-int values and non-existing pages are handled automatically. This appears to be compatible with DiggPaginator, also.
Cons of ListView
:
- No access to self.request (or self.session) outside of methods.