Multiple apps on one page, how?

1,461 views
Skip to first unread message

Michael Hipp

unread,
Dec 31, 2005, 12:02:15 PM12/31/05
to django...@googlegroups.com
Still trying to get my brain around how Django works...

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

Jacob Kaplan-Moss

unread,
Dec 31, 2005, 12:20:08 PM12/31/05
to django...@googlegroups.com
On Dec 31, 2005, at 11:02 AM, Michael Hipp wrote:
> How do I put multiple apps on one page/template?

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

Michael Hipp

unread,
Dec 31, 2005, 12:54:57 PM12/31/05
to django...@googlegroups.com

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


James Bennett

unread,
Dec 31, 2005, 4:35:09 PM12/31/05
to django...@googlegroups.com
On 12/31/05, Michael Hipp <Mic...@hipp.com> wrote:
> 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:

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

Luke Plant

unread,
Jan 2, 2006, 12:10:14 PM1/2/06
to django...@googlegroups.com

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/

Colin Howlett

unread,
Jan 2, 2006, 4:32:35 PM1/2/06
to Django users
In addition to what James and Luke have to say, you might want to look
at this thread on the developers list:

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.

Reply all
Reply to author
Forward
0 new messages