On Thu, 2009-01-08 at 22:58 -0800, itsnotvalid wrote:
> Second question at the same time....
> 
> Anyways, I encountered a problem that when I want to get an item from
> my media directory, which is set correctly in settings.py, however I
> couldn't find a template tag to get the url of the media. Looked into
> the code for djangoproject.com and found that they just quote the full
> url in base.html.
> That would be pretty weird to do hard coding since there is attributes
> in the settings file. Any hints?

There's a context processor available that can be used to supply the
media URL to your template's context. Have a look at 

http://docs.djangoproject.com/en/dev/ref/templates/api/#id1

and, in particular, django.core.context_processors.media. Every time you
use RequestContext as the context you pass to render_to_response (or
Template.render()), the context processors are all run and the media
one, if you include it, will put a particular variable (MEDIA_URL) in
your context that you can use to display that particular string.

A lot of people, as you notice, just include the string in the base
templates, since it doesn't really change that often (in fact, only if
you change the location of your static files, typically). So it's often
not an impractical solution (you don't *have* to use the setting URL,
after all). I do that on my blog, for example. Whether I'm serving from
my hosted system or using the development server, the static files are
always under /static/, so it's not particularly onerous to just write
that in the (single) base template.

Others might have some other little function that reads that setting and
passes it into the context. For example, I often have a common function
that almost all my views call instead of render_to_response(). This
function does some common tidying up operations and inserts some common
stuff into the context and then returns the result of
render_to_response().

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to