The Post is a Python object. A blog post needs a title which is a character field limited to 160 characters. It also needs a body which is the text and it is a text field (with no limitations). I added a date stamp (which is not mandatory but some people want it). I will comment it out later because I do not need it! The def __str__(self): will be used later on to list all of out blog posts.

This is it for the Post object. Of course, you may want to add categories and all other stuff a blog might have. You can include it within this class, create a new class, or organize it as you wish. For my website this is just fine.

Creating the urls.py for the blog app

The next step will be to create the urls.py within our blog app. It doesn’t exist, so create a file and name it urls.py and add to it the following lines of code:

We have to import the ListView and DetailView so we can display on our page a list of all posts and a detail view of each post. Of course, we have to import the Post model.

At the urlpatterns (which will start with /blog … see the mywebsite/urls.py) we will display a list view of all the blog posts, ordered by date (newest first) in the template named blog.html. We don’t have it but we will create it.

Creating the blog.html template

I created a folder named templates within the blog app. Within templates I created another folder named blog. Within templates/blog folder I created the blog.html file. Below you can see the entire code of blog.html:

The above code loops through all the posts and display the post date and the post title.

Before seeing anything in the browser we have to make migrations then to migrate. You should do that every time you add something to the website. You do it by running the following commands in the terminal.

python manage.py makemigrations

Then the command:

python manage.py migrate

Here are the screenshots of all the pages:

Our blog page is empty because there are no blog posts. We are going to register the blog application with our admin interface so we will be able to add posts through the admin interface provided by Django.

Below you can download the whole code of the application at this point in time: