Django Blog Tutorial – Make A Simple Blog

In this tutorial, we’ll discuss about how to make a simple Blog in Django 2.

Our blog will have TITLE, PICTURE, DESCRIPTION (Body), DATE.  There will be two pages, one for showing all the blogs at one place with less information and the second one to show each blog in detail.

Prerequisite

  • Django 2.0 or above should be installed.
  • Create a project

Django Blog Tutorial

Creating App

I have created a project named as blog_project. Now we’ll create an app for Blog, So in future, if we need to use Blog in other projects then we can use this app.

To create an app, open terminal and navigate to your project folder.

Type the command below:

python manage.py startapp blog

Now add a path for our newly created app in settings.py. Open settings.py and look for INSTALLED_APPS list.

Add these lines in the last.

I have added a path for Blog as first item of the list INSTALLED_APPS. If you’re thinking, from where this BlogConfig have came from instead of Blog, then navigate to your blog app folder and open apps.py file. Here you can see a class name as BlogConfig. This is what we add in settings.py while adding path for app in settings.

Creating Model

Open the models.py in your blog app folder and edit it as

So I’ve created a model to store title, publishing date (pub_date), picture (image) and description (body).  I’ve also created some functions inside the model.

  • Summary: summary functions will return us the short info about a blog. For example, on the page where we will show all the blogs, we’ll not display the whole body of each blog. Instead of it we’ll use short info about that blog.
  • pub_date_pretty: It will return you the date in this format – Sep 26, 2018, which looks more user friendly then the default date format.
  • __str__: It is an inbuilt function, I’ve overridden it to return the title instead of object index in the admin panel. You’ll see the result in the admin panel.

Now in the last, open the admin.py file to register your blog to show on the admin panel and the following lines.

Edit urls.py

Now edit your main urls.py file and add two url path. First for the homepage where all our blogs will display and second one is for detail page, where each of the blog in detail will be shown up.

Here we’ve added two paths, one for showing all the blogs and another for showing a particular blog in detail. The empty string in path shows that it is home page and will call views.allblogs function and ‘<int:blog_id>/means it will take integer after domain name and call views.detail function.

Edit views.py

Open the views.py inside your app blog folder and edit it as:

Firstly we are importing some of modules to work with,

  • render: to return a template on requested.
  • get_object_or_404: to get the stored objects from database.
  • Blog: to get the Blog’s objects we have to use this model name.

After that, we have two functions allblogs() and detail().  allblogs function will get all the stored objects from database of Model Blog and return the template along with all the objects of database.

detail function have an extra parameter inside it – blog_id, this blog_id will be used to get a particular object from the database according to the primary key and the primary key is our blog_id.

After that this function will return user another template along with the particular object.

Create Templates

  • allblogs.html: create a folder templates inside the blog app, then create another folder inside the template as the same name as app blog, now create allblogs.html here.

So our project directory will look like this:

Django Blog Tutorial 1

And add this code into you allblogs.html:

In above code, we’re looping through all the blogs (sent from allblogs function in the views) and printing title of the blog as link, date, image and short summary.

When user click on the title, he/she will be navigated to the URL having name detail.

  • details.html: create details.html in the same directory as above and edit it as:

In above code, we’re just printing the title, publishing date, image , full summary of the particular blog.

Testing

First migrate unapplied migrations, open your terminal and navigate to the project folder and run:

python manage.py migrate

Then run server by typing:

python manage.py runserver

Now open browser and open admin panel (http://localhost:8000/admin/) and login to add some data into our databases.

In case, you don’t have the admin username and password, just stop your server and type this command:

python manage.py createsuperuser

And create admin account. Then reload the server and open admin panel to add some blog posts.

Django Blog Tutorial 2

Django Blog Tutorial 3

 

Django Blog Tutorial 3

As you can see I have added three blogs. Now let’s open our website’s homepage.

Django Blog Tutorial 5

After clicking on any title:

Django Blog Tutorial 6

Its working. That’s the simplicity of Django. We can make it more attractive by adding some bootstrap or css.

If you’ve any query related with this article, please let us know in comment box.

4 thoughts on “Django Blog Tutorial – Make A Simple Blog”

Leave a Comment

Your email address will not be published. Required fields are marked *