I have a base template (main.html
) with the following code for the tag:
main.html:
<title>{% block title %}{% endblock %} | Application</title>
In a child template (dashboard.html
), I override the title block like this:
dashboard.html:
{% extends 'main.html' %}
{% block title %}Dashboard{% endblock %}
If the title
block is not overridden in the child template, I want the title to just be Application, without the extra | Application
. How can I check if the block is overridden and modify the title accordingly?
When I pass the title block from the child template like this:
{% extends 'main.html' %}
{% block title %}Dashboard{% endblock %}
Then I need the title to display as "Dashboard | Application"
.
If I don't pass the title block:
{% extends 'main.html' %}
Then the title should just be "Application"
.
I have a base template (main.html
) with the following code for the tag:
main.html:
<title>{% block title %}{% endblock %} | Application</title>
In a child template (dashboard.html
), I override the title block like this:
dashboard.html:
{% extends 'main.html' %}
{% block title %}Dashboard{% endblock %}
If the title
block is not overridden in the child template, I want the title to just be Application, without the extra | Application
. How can I check if the block is overridden and modify the title accordingly?
When I pass the title block from the child template like this:
{% extends 'main.html' %}
{% block title %}Dashboard{% endblock %}
Then I need the title to display as "Dashboard | Application"
.
If I don't pass the title block:
{% extends 'main.html' %}
Then the title should just be "Application"
.
1 Answer
Reset to default 2You can't do that in plain Django, but you can work around it by changing your main block to
<title>{% block title %}{% endblock %}Application</title>
and overriding it like
{% block title %}Dashboard | {% endblock %}
Alternatively, you can use the {% include %}
tag instead of {% block %}
:
title.html:
<title>{% if title %}{{ title }} | {% endif %}Application<title>
then
dashboard.html:
:
{% include "title.html" with title="Dashboard" %}
django-view-breadcrumbs
does for breadcrumbs: github/tj-django/django-view-breadcrumbs – willeM_ Van Onsem Commented Feb 17 at 5:44null
pointer of course). – willeM_ Van Onsem Commented Feb 17 at 6:35if..else
tags in templates, and if those don't work you can build your own. What you are asking is basically how to check if Djangos template engine is running (that's how I read it), yes it is...why not focus on creating the variable you need where it is supposed to be created? It's also easier for developers to change logic in views normally as opposed to templates, kind of the whole point of MVC – ViaTech Commented Feb 18 at 2:21