Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between {% load staticfiles %} and {% load static %}

The most important part of the question is in the topic.

I am wondering what tag is best for which case. Moreover... I found code, that also use settings.STATIC_URL included by {{STATIC_URL}} in the templates.

I am a little confused.

like image 409
trikoder_beta Avatar asked Jun 16 '14 07:06

trikoder_beta


People also ask

What is {% load static %} in Django?

The {% static %} template tag generates the absolute URL of static files. That's all you need to do for development. Reload http://localhost:8000/polls/ and you should see that the question links are green (Django style!) which means that your stylesheet was properly loaded.

What is {% static %} means in Python?

{% load static %} tells django to load a set of template tags/filters defined in the file static.py (in a folder templatetags in any of the apps of your project). The same way you can define your own tags, put them in a file util_tags.py and load them with {% load util_tags %} . – dirkgroten. Dec 12, 2019 at 15:58.

What does load static mean?

Static loading is any load that is applied slowly to an assembly, object or structure. Static loads are also those that remain consistent and do not move at all. They are used to work out the maximum load for a range of structures and objects as well as for determining the ultimate tensile strength of materials.

What does {% load static %} do in HTML?

The very first line in the file, {% load static %} , uses Django's special template tag syntax to tell the template engine to use the files in the static folder in this template.


2 Answers

The built-in static template tag "link[s] to static files that are saved in STATIC_ROOT".

The staticfiles contrib app's static template tag "uses the configured STATICFILES_STORAGE storage to create the full URL for the given relative path", which is "especially useful when using a non-local storage backend to deploy files".

The built-in static template tag's documentation (linked to above) has a note that says to use the staticfiles contrib app's static template tag "if you have an advanced use case such as using a cloud service to serve static files", and it gives this example of doing so:

{% load static from staticfiles %} <img src="{% static "images/hi.jpg" %}" alt="Hi!" /> 

You could use {% load staticfiles %} rather than {% load static from staticfiles %} if you want, but the latter is more explicit.

like image 112
Nick Avatar answered Oct 03 '22 22:10

Nick


I don't know what the difference is supposed to be, but I found a use case difference (using django 1.9.1 running via apache, wsgi on Python 3.4). In my app, I have some images in ImageFields in the database. If I use code like this in my template:

<a href="object-{{object.id}}"><img src="{% static object.image %}" height="200px"></a> 

then, if I use {% load static %}, django throws a TypeError (Cannot mix str and non-str arguments). This is presumably because the object.image is not a string, it's an ImageField, that gets converted to a string at some later stage. However, if one uses {% load staticfiles %} no such error occurs.

Unfortunately, I discovered this difference after spending hours trying to debug the problem. I managed to find a workaround for when using the first option, namely to add a string-converter method to the object like this:

#image string def image_str(self):     return str(self.image) 

Hope this knowledge will be of use to someone.

like image 42
CoderGuy123 Avatar answered Oct 04 '22 00:10

CoderGuy123