最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to conditionally modify the <title> tag in Django templates? - Stack Overflow

programmeradmin3浏览0评论

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

Share Improve this question edited Feb 17 at 4:27 Selcuk 59.3k12 gold badges110 silver badges115 bronze badges asked Feb 17 at 3:14 Ali ZainAli Zain 255 bronze badges 5
  • I would really advice not to do such logic in the template. You can work with some logic to render the view properly. To some extent what django-view-breadcrumbs does for breadcrumbs: github/tj-django/django-view-breadcrumbs – willeM_ Van Onsem Commented Feb 17 at 5:44
  • @willeM_VanOnsem I think it's not called logic it's a simple thing that can be easily done with ASP.NET Core and Laravel. then Why not Django or Jinja – Ali Zain Commented Feb 17 at 5:50
  • @AliJinja: because as said, Django deliberately tries to prevent bad practices. Evidently that is logic, programming is logic. As a manager of me once said: "If I receive a CV with .NET or Java on it, it is a simple desk reject.". Templates should be concerned with what you render, you typically should hand the data to the template renderable without any transformation. The template only injects the data into the HTML, that is the sole purpose of a template. – willeM_ Van Onsem Commented Feb 17 at 6:33
  • This looks a bit like the goto statement considered harmful discussion. Where once thinks, "aha, goto, an extra feature". But this was a billion dollar mistake (together with a null pointer of course). – willeM_ Van Onsem Commented Feb 17 at 6:35
  • @AliZain perhaps explaining why you want to do this is a better option to get a response? Obviously you shouldn't be rendering complex logic in templates, Django provides default if..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
Add a comment  | 

1 Answer 1

Reset to default 2

You 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" %}
发布评论

评论列表(0)

  1. 暂无评论