J.W
J.W

Reputation: 719

Django: how to set up the site name and show it at the end of all pages title

For example, there are 3 html templates with different titles:

<title>Page1</title>
<title>Page2</title>
<title>Page3</title>

How to append all the titles with the site name at the end using variable (or other ways)?:

<title>Page1 - {{Here is the configurable Site Name}}</title>
<title>Page2 - {{Here is the configurable Site Name}}</title>
<title>Page3 - {{Here is the configurable Site Name}}</title>

Upvotes: 0

Views: 670

Answers (1)

Michael Anckaert
Michael Anckaert

Reputation: 953

You normally do this by setting the site name in a base template that you then extend from your other templates.

Suppose you have this base.html template

<html>
<head>
   <title>{% block page-title %}{% endblock %} - My Site</title>
</head>
...

You can then extend this template from your other pages:

{% extends "base.html" %}
{% block page-title %}My first page{% endblock %}
...

This solution is what is normally seen in many Django projects.

Now if you want to make the site name configurable, from your Django settings for example, you'll need a way to pass it along to your view for rendering. Since this is something you'll be doing for every view, it's better to do this in context processor so you don't have to do it manually.

Upvotes: 2

Related Questions