how to write re-usable views in django?
- by rz
These are the techniques that I use regularly to make my views reusable:
take the template_name as an argument with a default
take an optional extra_context which defaults to empty {}
right before the template is rendered the context is updated with the extra_context
for further re-usability, call any callable in extra_context.values()
whenever the view deals with a queryset, there is a queryset argument with a default
whenever the view needs a particular object from the ORM, it attempts to fetch it using any "id" parameter in several ways (e.g. as a slug, as a database id) (this may be a bad practice...)
First, Should I add anything to my list? Should I remove anything from my list?
The items accommodates a large number of cases. However, whenever an app extends a model of another in some way (e.g. adding a field or changing the behavior in some way) I end up writing my own views and only reusing the model. Is this normal?