Static/media url in template

1,772 views
Skip to first unread message

Jon J

unread,
Apr 4, 2011, 6:44:03 PM4/4/11
to django...@googlegroups.com
Maybe I'm missing something...

I would say I'm relatively familiar with the Django framework by this
point. I'm mocking up a website, and I want to link static files into
the base template. Instead of having the url's directly in the
template, I'd like to call the STATIC_URL variable from within the
template, so for example I could have

<link rel="style" type="text/css" href="{{ STATIC_URL }}style.css" />

instead of

<link rel="style" type="text/css" href="http://www.domain.org/style.css" />

Is this something that's possible or advisable? How is this problem
normally handled so I don't have to retype pretty much everything if I
move my site around? I haven't been able to find anything in the
documentation regarding this.

Sam Walters

unread,
Apr 4, 2011, 8:49:50 PM4/4/11
to django...@googlegroups.com
Yes it already exists {{MEDIA_URL}}

Thats what the settings.py file does...

There is a bunch of stuff you can read about in the docs:

http://docs.djangoproject.com/en/1.3/howto/static-files/

Also for some of my own deployments there are a bunch of static file
servers so i just put these into settings:
{{MEDIA_URL_VIDEO}}
{{MEDIA_URL_IMAGES}}
etc...

Basically its as extensible as you want to make it.

cheers

sam_w

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

Jon J

unread,
Apr 5, 2011, 10:39:17 AM4/5/11
to django...@googlegroups.com
It doesn't appear that simple. When I try to insert {{MEDIA_URL}} into
my template, I get no output whatsoever. I haven't been able to find
anything on this.

Daniel Roseman

unread,
Apr 5, 2011, 10:44:46 AM4/5/11
to django...@googlegroups.com
On Tuesday, April 5, 2011 3:39:17 PM UTC+1, JohnnyWalker wrote:
It doesn't appear that simple. When I try to insert {{MEDIA_URL}} into
my template, I get no output whatsoever. I haven't been able to find
anything on this.

You don't seem to be looking very hard.

(and the version for 'static' directly underneath)
--
DR.

Ryan Osborn

unread,
Apr 5, 2011, 10:47:34 AM4/5/11
to django...@googlegroups.com
You need to have django.core.context_processors.media in your TEMPLATE_CONTEXT_PROCESSORS setting in order to use this template tag.

The same with STATIC_URL, you need to have
django.core.context_processors.static in order to use it.

Hope this helps,

Ryan

Jon J

unread,
Apr 5, 2011, 10:57:08 AM4/5/11
to django...@googlegroups.com
N/M, I solved it. I found this little gem:

from django.template import RequestContext

[code...]
return render_to_response('base.html',locals(),
context_instance = RequestContext(request))

which gives me access to the URL from the templates. This should
definitely be in the documentation in the basic templating tutorial
because up until I've found this, everyone has been acting like
MEDIA_URL is accessible with no additional steps.

Reply all
Reply to author
Forward
0 new messages