Creating Django Personal App – Part 4 – Creating the blog app

It is time to implement the blog functionality to our website. Although I could make the blog within the personal app, I decided to make a separate app called blog.

Creating the blog app

To create a new app within a Django project, run the following command in the terminal:

python manage.py startapp blog

Register the blog app in the settings.py file

As with any new app of the project, you have to register it in the settings.py file. The INSTALLED_APPS part of the settings.py file will look like shown below:

INSTALLED_APPS = [
    'personal',
    'blog',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Creating the URL to the blog app

The next important thing you have to do with every new app is to create the URL to it. So, open the urls.py and add the URL as shown below:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('personal.urls')),
    url(r'^blog/', include('blog.urls')),
]

Creating the model for our blog posts

We will have to create the urls.py within the blog app, but first let’s create our model for the blog. So, open and edit the models.py within the blog app.

Before start doing anything within the models.py file, we need to think what a blog needs. First thing first, a blog needs posts. That is the first thing to be created within the blog/models.py file.

from django.db import models

# Create your models here.

class Post(models.Model):
    title = models.CharField(max_length = 160)
    text = models.TextField()
    date = models.DateTimeField()

    # Meta Information
    def __str__(self):
        return self.title

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:

from django.conf.urls import url, include
from django.views.generic import ListView, DetailView
from blog.models import Post

urlpatterns = [
    url(r'^$', ListView.as_view(queryset=Post.objects.all().order_by("-date")[:25], template_name="blog/blog.html"))
]

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:

{% extends "personal/header.html" %}
{% block content %}
	{% for post in object_list %}
		{{ post.date|date:"Y-m-d" }}
		<a href="/blog/{{post.id}}">  {{ post.title }}</a><br>
	{% endfor %}
{% endblock %}

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:

Home page Django

About page Django

Portfolio page Django

Blog page Django

Contact page Django

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:

MyWebsite (after part 4)

Leave a Reply