how to write re-usable views in django?

Posted by rz on Stack Overflow See other posts from Stack Overflow or by rz
Published on 2010-03-31T03:25:08Z Indexed on 2010/03/31 3:33 UTC
Read the original article Hit count: 361

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about django

Related posts about python