I've been using {% get_media_prefix %}
for a very long time. I was explaining this to someone when he pointed this out.
Why do I need to declare {% load static %}
in order to use it?
It even uses in the documentation's example code here.
To an extent I understand that static files and media files are similar in nature. Even when we use them with combination of nginx+gunicorn, nginx handles both of them(we let everything else proxy, but not these).
But still we have a separate MEDIA_URL
and STATIC_URL
as well as MEDIA_ROOT
and STATIC_ROOT
defined for these files.
Then why {% load static %}
needs to be declared in order to use {% get_media_prefix %}
?
Thanks in advance.
In order to use a template tag in your HTML you must first load the module that contains it.
So, accorrding to the source code of
get_media_prefix
template tag, this template tag lives insidedjango/templatetags/static.py
.That's why you have to
load
it every time you use it, in every HTML template.This, of course, applies to every template tag usage. At the top of every HTML file you load the template tags and then you use them. Just like you
import
a function in your python code.UPDATE: From the Django 1.3 release notes:
As you can see, Django used to treat both static and media the same. Since Django 1.3 this changed, but the template tag not. No big deal. It's just a convention.