Is it good practice to treat individual app views as a blocks of HTML that can be pieced together to form a larger site? If not, what is the best way to reuse app views from project to project, assuming each one uses a different set of templates?
+2
A:
A general good practice to define views with a template_name
kwarg. This allows a the default template to be overridden. This is common in generic views.
#my reusable view
def list_items(request, template_name="items.html"):
items=Item.objects.all()
return render_to_response(template_name,
{'items': items},
context_instance=RequestContext(request))
#some other view
from my.reusable.views import list_items
def list_special(request, template_name="spectial_items.html"):
return list_items(request, template_name=template_name)
czarchaic
2009-12-29 18:32:03
A:
Your question is little too generic.
The general way of doing it involves:
- Extend templates of the reusable apps
- Pass the new template name to the view (Reusable apps should accepts that argument)
- Also pass
extra_context
to the reusable-generic-view - Use your own view to create an
extra_context
and return the reuse-able view, from your view.
Lakshman Prasad
2009-12-29 18:39:30
A:
usually each application should provide views for the basic functionality - where the application takes full control over the user and the page.
functionality which can be displayed in the basic page layout (e.g. 'last 5 posts in my blog') would be a perfect use case for template tags - i usually use simple inclusion tags. so you wouldn't combine multiple views into one template, but always have one view which handles the request, and everything around can be aggregated using template tags.
herbert
2009-12-29 21:23:45