Search code examples
djangodjango-templatesdjango-staticfilesstatic-files

Do I really need to use {% load static %} in Django Templates?


I am using Django 3, the latest version. I have defined the static files directories properly as required. Currently to display an image, both the following source codes work fine.

Code 1:

<img src="static/img/logo.png">

Code 2:

{% load static %}
<img src="{% static 'img/logo.png' %}">

Since both the code snippets are working and running fine without any problems, I am wondering why not to simply use the way of Code 1 above and refrain from the extra lines of Code 2.

Which one would be beneficial and why? Please guide.


Solution

  • The base/master level templates would work without static tag and maybe considered for performance.

    However for other level templates either nested or in app templates or nested urls, it's required to be reference with static as it would minimise chances of error. So for all other templates it is recommended to use static tag.