How do I put multiple apps on one page/template?
Explanation:
I have my base.html template, different parts of the page need to be fed data
from different apps (app1, app2, etc.) The apps are independent and don't
(shouldn't) know anything about one another. The template has blocks/variables
to grab the data from each app.
Issue:
The urlpatterns all seem to be geared around a one-one (url->view) model. The
associated template can inherit from others that have additional things on
them but how do I get the views for those other apps to run?
Hope that made some sense.
Thanks,
Michael
Just use both apps in the same view::
from django.models.polls import polls
from django.models.blogs import entries
def my_view(request):
return render_to_response("template_name", {
"poll_list" : polls.get_list(),
"entry_list" : entries.get_list(),
}
Jacob
Thanks. But isn't this going to violate the DRY principle?
I'm thinking of the example where I have things that I want to appear around
the perimeter of every page on my site representing the output of numerous
independent apps. This would seem to leave me with two choices:
1) Replicate the same code across a lot of different views in different apps.
2) Built a mega-view that is called for every url pattern in which it is
decoded what was really requested and then calls the various views.
Neither of those sound like good options.
Any insight appreciated,
Michael
Try writing template tags for your other applications; for example,
say you're a web designer and you have a site with two apps: a weblog
and a portfolio, and you want to show a list of the five latest weblog
entries on every page, even pages in the portfolio. You could write
massively complicated views, yes, or you could write a template tag in
your weblog application, and use it in all your page templates.
Template tags can do a lot of the same things views can, including
fetching things from the database, so this would probably be your best
solution.
See the template tags documentation for more on how to write your own
tags: http://www.djangoproject.com/documentation/templates_python/
--
"May the forces of evil become confused on the way to your house."
-- George Carlin
Template inheritance should cover the template side of things, and for
the views, why not have a single function like that gets the context
variable for all the 'extra' things around the edge:
c = {"poll_list": polls.get_list()}
c.update(get_extra_standard_stuff(request))
render_to_response("template_name", c)
The only thing that is then repeated is the call to
get_extra_standard_stuff(). And you can eliminate that if you use
DjangoContext and this feature:
http://www.djangoproject.com/documentation/settings/#template-context-processors
Luke
--
"Because Your lovingkindness is better than life,
My lips shall praise You." (Ps 63:3)
Luke Plant || L.Plant.98 (at) cantab.net || http://lukeplant.me.uk/
http://groups.google.com/group/django-developers/browse_frm/thread/53ce5282c9e29df8/
You should mostly ignore my incoherent ramblings there, but Robert
Wittams identified an elegant solution to my problem which I think is
the same one you have.
Hope that helps.