[Python] How to conditionally modify the <title> tag in Django templates?

Discussão em 'Python' iniciado por Stack, Fevereiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    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".

    Continue reading...

Compartilhe esta Página