Create a blog application with django, part 6: create django templates

Now that We have created views and URL patterns for the blog application. It's time to create the HTML templates to display posts in a user-friendly manner. The render() function looks for HTML templates inside a directory called templates inside your app directory.

Create the following directories and files inside your blog application directory:

(venv) mypc:~/venv/mysite/blog$ mkdir templates

$ cd templates

$ touch base.html

$ mkdir blog

$ cd blog

$ touch post_list.html post_detail.html

# mysite/blog/templates



The preceding structure will be the file structure for our templates. The base.html file will include the main HTML structure of the website and divide the content into the main content area and a sidebar. The post_list.html and post_detail.html files will inherit from the base.html file to render the blog post list and detail views, respectively.

Django has a powerful template language that allows you to specify how data is displayed. It is based on template tags, template variables, and template filters:

  • Template tags control the rendering of the template and look like {% tag %}.
  • Template variables get replaced with values when the template is rendered and look like {{ variable }}.
  • Template filters allow you to modify variables for display and look like {{ variable|filter }}.

You can see all about Django built-in template tags and filters here .

Let's edit the base.html file and add the following code:

<!-- mysite/blog/templates/base.html -->

{% load static %}
<!DOCTYPE html>
 <title>{% block title %}{% endblock %}</title>
 <link href="{% static 'css/blog.css' %}" rel="stylesheet">
 <div id="content">
   {% block content %}
   {% endblock %}
 <div id="sidebar">
   <h2>My blog</h2>
     <p>This is my blog.</p>

{% load static %} tells Django to load the static template tags that are provided by the django.contrib.staticfiles application, which is contained in the INSTALLED_APPS setting. After loading it, you are able to use the {% static %} template tag throughout this template. With this template tag, you can include static files, such as the blog.css file. This file doesn’t exist yet, but we’ll create it soon.

You can see that there are two {% block %} tags. These tell Django that we want to define a block in that area. Templates that inherit from this template can fill in the blocks with content. We have defined a block called title and a block called content.

Let's edit the post_list.html file and make it look like the following:

<!-- mysite/blog/templates/blog/post_list.html -->

{% extends "base.html" %}

{% block title %}My Blog{% endblock %}

{% block content %}
 <h1>My Blog</h1>
 {% for post in posts %}
     <a href="{{ post.get_absolute_url }}">
       {{ post.title }}
   <p class="date">
     Published {{ post.publish }} by {{ }}
   {{ post.body|truncatewords:30|linebreaks }}
 {% endfor %}
{% endblock %}

With the {% extends %} template tag, we tell Django to inherit from the base.html template. Then, we are filling the title and content blocks of the base template with content. We iterate through the posts and display their title, date, author, and body, including a link in the title to the canonical URL of the post. In the body of the post, we are applying two template filters: truncatewords truncates the value to the number of words specified, and linebreaks converts the output into HTML line breaks. You can concatenate as many template filters as you wish; each one will be applied to the output generated by the preceding one.

To create the blog.css file. First, we have to create a directory called static in your blog application directory. Django will look for static files there, similarly to how Django finds templates inside blog/templates/.

Create the following directories and file inside your blog application directory:

(venv) mypc:~/venv/mysite/blog$ mkdir static

$ cd static

$ mkdir css

$ cd css

$ touch blog.css

# mysite/blog/static



Let's edit the blog.css file and make it look like this:

/* blog/static/css/blog.css */

body {
   font-family:helvetica, sans-serif;

a {

h1 {
   border-bottom:1px solid #bbb;
   padding:0 0 10px 0;

h2 {
   margin:30px 0 0;

#content {
   padding:0 0 0 30px;

#sidebar {
} {
   font-family: georgia, serif;
   font-size: 12px;
   font-style: italic;

Open the shell and execute the python runserver command to start the development server. Visit in your browser, and you will see everything running. Note that you need to have some posts with the Published status to show them here. You should see something like this:

Responsive image

Then, let's edit the post_detail.html file:

<!-- mysite/blog/templates/blog/post_detail.html -->

{% extends "blog/base.html" %}

{% block title %}{{ post.title }}{% endblock %}

{% block content %}
 <h1>{{ post.title }}</h1>
 <p class="date">
   Published {{ post.publish }} by {{ }}
 {{ post.body|linebreaks }}
{% endblock %}

Return to your browser and click on one of the post titles to take a look at the detail view of a post. You should see something like this:

Responsive image

Take a look at the URL, it should be /blog/2020/1/11/python-my-favourite-programming-language/. We have designed SEO-friendly URLs for our blog posts.

That's it for now. The next tutorial is to add pagination to our blog.

If you like my content, please consider buying me a coffee.
Thank you for your support!

Related posts