views:

232

answers:

3

Lets say I have a website with a small application that lists the 10 newest members, or something dynamic like that. I want this to view on every page, perhaps in a sidebar. How would i go about doing this.

My thoughts on the mather...
I might not get the whole django thing just yet, but when I have a url like /foo/ calling a view bar - but what info do I have to send to the template from this view. Does every view have to send the info to the template (just so I can view my app) or is there someway to call this from the template instead.

I have tried to read through the documentation, but its seems I just can't understand this.

Thanks

+1  A: 

"Does every view have to send the info to the template (just so I can view my app)"

Yes.

"Is there someway to call this from the template instead."

No.

Your views are just functions. Functions can call other functions. That's ordinary good design. You can still do ordinary good design in Django.

You do have the ability to provide a "context". This is still done in the views to provide additional "context" for the templates. See http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors for writing your own context processor.

Nothing (well almost nothing) is done in the template except render the objects provided by the view into HTML (or XML).


If you have a page that is an amalgamation of stuff from many small apps, then you have two tiers of apps.

  • Independent Apps.

  • Composite Apps that depend on Composite or Independent Apps.

Your composite app can call other app view functions to gather data.

Your composite app template can include other app template elements to present that data.

You have all the power of Python to decompose the independent apps into "data production" functions, view functions, template components and final page templates.

An independent app Page will use a view function and a template. The view function will use the data production functions. The template will use template components.

Decomposition still works, even in Django.

S.Lott
Nothing? Template tags do something.
Oli
Okey, I was afraid of that. But how would you do this. You have a few small applications like this newest users, perhaps a few others. How would you structure this to make it easy. I mean DRY and all. Would you make a function that calculates the newest users, and call that from your view, and then send the data to the template. If so, how is this done in the most "sensible" way?thanks for the answer by the way
Fifth-Edition
@Oli: Template tags can iterate and make simple decisions. They shouldn't be a replacement for proper view logic. Yes, they *can* do quite a bit. But performance usually suffers.
S.Lott
-1. Making this much harder than it needs to be.
Daniel Roseman
+1  A: 
  1. Create a template tag that you can call on the page
  2. Create a context processor and inject extra context variables onto each pageload

I'm sure there are other ways of doing this but those are probably the most logical two. The first gives you more power and will waste less processing time (for pages where you don't want to display the data) but a context processor is much more simple to write (you don't have to bend over backwards to please the template_tag gods).

Both are valuable things to know so there you go. Go and learn!

Oli
Context processor should be #1. A template tag that "does work" (like a query) can turn into a hard-to-test-and-debug performance problem more quickly than a context processor will.
S.Lott
Completely, absolutely disagree with S.Lott. Context processors are strictly for low-impact things - making available settings, media url, that sort of thing. If you need to do work, a template tag is the way to go. With the context processor doing it, what if you suddenly need a page - or section - without that query? It's being done on all pages and there's no way of turning it off.
Daniel Roseman
Of course, if they fixed the _resolve_lookup function in the template variable handling code so it dealt with first-level callables correctly then you could create a curried function and stick it into the context and it would only do the work if you actually invoked it. This is a 10 line patch I have to keep making on every project. Alternatively, use jinja2 for templating and just do the python expression where it's needed in the template.
Peter Rowell
+3  A: 

The usual way to provide '10 newest members' type of information from other apps is via a template tag. See this article by James Bennett on best practices (although note it's a bit out of date, as it was written before the inclusion_tag and simple_tag shortcuts were available).

Daniel Roseman
Thank You!!! This was exactly what I looked for!
Fifth-Edition