access MEDIA_URL in templates

2 views
Skip to first unread message

du...@habmalnefrage.de

unread,
Aug 12, 2006, 8:13:19 AM8/12/06
to django...@googlegroups.com
Hi all,

is it possible to get the value of MEDIA_URL from settings.py in the templates ?

Regards,
Dirk
--


"Feel free" – 10 GB Mailbox, 100 FreeSMS/Monat ...
Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail

limodou

unread,
Aug 12, 2006, 9:58:12 AM8/12/06
to django...@googlegroups.com
On 8/12/06, du...@habmalnefrage.de <du...@habmalnefrage.de> wrote:
>
> Hi all,
>
> is it possible to get the value of MEDIA_URL from settings.py in the templates ?
>
> Regards,
> Dirk
You can pass it manually through Context, for example:

from django.conf import settings
...
return render_to_response("test.html", {'media_url':settings.MEDIA_URL})

--
I like python!
My Blog: http://www.donews.net/limodou
My Django Site: http://www.djangocn.org
NewEdit Maillist: http://groups.google.com/group/NewEdit

James Bennett

unread,
Aug 12, 2006, 10:25:50 AM8/12/06
to django...@googlegroups.com
> is it possible to get the value of MEDIA_URL from settings.py in the templates ?

You can write a template context processor, and use the
`RequestContext` subclass of the normal Context.

See the official documentation (and list of built-in context processors) here:
http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext

I've also written a tutorial which explains this in more detail, and
actually uses MEDIA_URL as the example:
http://www.b-list.org/weblog/2006/06/14/django-tips-template-context-processors

--
"May the forces of evil become confused on the way to your house."
-- George Carlin

du...@habmalnefrage.de

unread,
Aug 12, 2006, 11:06:36 AM8/12/06
to django...@googlegroups.com
Hi,

thanks for the hints, that exactly what I was looking for.

May I'm not the first who asked this question.
The solution maybe should be placed somewhere on the django-wiki ?
Or the context_processor.media_url should go into the django-distro ?

Regards,
Dirk
--


Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer

Bryan Chow

unread,
Aug 12, 2006, 11:51:46 AM8/12/06
to django...@googlegroups.com
+1 for James' excellent tutorial.

Here's the context processor that I use in most of my projects:
http://www.verdjn.com/wiki/Siteroot
http://www.verdjn.com/browser/verdjnlib/context_processors/__init__.py

Reply all
Reply to author
Forward
0 new messages