2

I'm on a project in Django, and the problem is like this. My site have a overall layout like 'layout.html' which includes some static files, links in and 'header.html' including navigator, 'footer.html' including some links in . But Both of 'layout.html' and 'header.html' uses static files so I think one command {% load static %} on first line in 'layout.html' can affect both 'layout.html' and 'header.html' because 'layout.html' includes 'header.html'! But it doesn't work, it works only when {% load static %} in both files. Maybe my explanation is hard to understand, so I will write my code very shortly.

'layout.html'

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<link href="{% static 'onepage-scroll.css' %}" rel="stylesheet" type="text/css">
</head>
    <body>
        {% include 'header.html' %}
        {% block content %}
        {% endblock %}
        {% include 'footer.html' %}
    </body>
</html>

'header.html'

{% load static %}
<nav id="navi">
    <h3><a href="{% url 'main:home' %}"><img src="{% static 'logo.jpg' %}"></a></h3>
</nav>

I think this is not a good implementation because there are 2 times of loading staticfiles. Is there another way to solve this?

1 Answer 1

2

I think you may be misunderstanding what {% load static %} does. It just gives your template access to the associated {% static %} tag. It does not result in the static files themselves being reloaded or any significant burden in terms of extra code being ported.

You're using the tag exactly as intended. If you need static files, you need to load the tags to do that with.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.