DEV Community

Cover image for Page Title and Favicon in Django
balt1794
balt1794

Posted on

Page Title and Favicon in Django

I will assume that you already have a Django web app set up, so we will dive right into how to add a page title and favicon to a Django website.

We only need two lines of code to do this.

Page Title

Let’s start with the page title. If you are using the local server, you should have something similar to this when running.

Screen-Shot-2021-06-25-at-7.07.12-PM-1

To change the page title, add the following line of code at the top your HTML template.

If you have any imports at the top, add the line after the imports.

I chose the name of my app (DEVBALT) for this example.


<title>DEVBALT</title>


 <!-- baltlogs.com-->

Enter fullscreen mode Exit fullscreen mode

After refreshing the page, you should see the page title.

Screen-Shot-2021-06-25-at-6.42.04-PM

There are two ways to do this. You can create a base template and have one page title for all pages of the website, or you can add a page title to each template depending on the page.

For example, I chose to display the name of my website (DEVBALT) for all pages on my website. However, you can choose to display CONTACT for the contact page, BLOG for the blog page, etc.

Favicon

Let’s tackle the favicon.

Django uses a variation of HTML templates which makes it easy to add the favicon. However, the line below won’t work.


<title>DEVBALT</title>

<link rel="shortcut icon" type="image/png" href="img/favicon.png">


 <!-- baltlogs.com-->

Enter fullscreen mode Exit fullscreen mode

First, we need to set up static files for the website.

If you already did this, you can skip this part.

If not, follow the tutorial below to set up static files.

Static Files Setup

Create a new folder named img inside the static folder.

Move your favicon file (png or any other acceptable format) into the img folder.

Depending on the browser, you’ll see a default icon next to the name of the website.

If using Google Chrome, you’ll see a planet as the default icon showing next to the name of the website.

Change the line shown earlier to the following.


<title>DEVBALT</title>

<link rel="shortcut icon" type="image/png" href="{% static 'img/favicon.png' %}">


 <!-- baltlogs.com-->

Enter fullscreen mode Exit fullscreen mode

In the above line, we added the path to the favicon file in a way that Django understands.

Now if you refresh the website, you should see the favicon appear.

Screen-Shot-2021-06-25-at-6.38.42-PM

Twitter

Registration Page using UserCreationForm (Django) – Part 1

Registration Page using UserCreationForm (Django) – Part 2

Top comments (0)