Django - copy header from admin to all templates

So what I want to do is add the django admin header to my own base template for my project. I copied the base.html file from the admin templates into my project. Can I somehow put the {% block header%} tags in base.html and then call it in my own base template for my project?

{% block header %} <!-- Header --> <div id="header"> <div id="branding"> {% block branding %}{% endblock %} </div> {% if user.is_active and user.is_staff %} <div id="user-tools"> {% trans 'Hi,' %} <strong>{% filter force_escape %}{% firstof user.first_name user.username %}{% endfilter %}</strong>. {% block userlinks %} {% url 'django-admindocs-docroot' as docsroot %} {% if docsroot %} <a href="{{ docsroot }}">{% trans 'Documentation' %}</a> / {% endif %} <a href="{% url 'admin:password_change' %}">{% trans 'Change password' %}</a> / <a href="{% url 'admin:logout' %}">{% trans 'Log out' %}</a> {% endblock %} </div> {% endif %} {% block nav-global %}{% endblock %} </div> <!-- END Header --> {% endblock %} 
+4
source share
2 answers

I ended up expanding my main template from the admin template 'base.html' and went from there. A bit dirty but it works

0
source

All templates that extend from 'base.html' will contain content inside {% block header %} and {% endblock %} if they do not override the block or its ancestors (by deleting part of {% block header %}...{% endblock %} ).

If there are templates that do not apply to base.html , you can put the code in your shared database or use something like an inline tag or inclusion tags

In addition, for the code to work correctly for authenticated users, you need to make sure that there is a user variable in the context: it usually already exists, or you need to enable "django.contrib.auth.context_processors.auth" again if you have it deleted before, check the document

0
source

All Articles