How to Build Simple Blogging System using Django-Part-3 Creating Views and Routing URL


Posted on 02 February 2017

Hello Coders and Welcome back to the another tutorial of the series this is going to be third part and in this tutorial we are going to learn about views, how to create them and we will learn how to do URL routing

If you have not gone through the First and Second part of the series we stongly recommend you to go through them first. Links are given below.

Part 1:

Part 2:

blogging system django url routing and views

In this tutorial we are basically going to build a page which a user can open in a web browser and can see all the blog posts which are published by the author, we have already explain views in the previous tutorials so we will not waste more time and we will straight dive into creating views but before that we will setup our URL at which our view will be opened so let's start.

Open the following directory in your browser BlogSystem\blogapp and create a new file called  and type in the following code into it.

from django.conf.urls import url

from . import views

urlpatterns = [

In the above code we are simply creating a url pattern which will represent http://localhost:8000/blogapp/ and http://localhost:8000/blogapp/slug-url-come-here which will tell the browser to open the index and show_article function defined inside the

Now go to the following directory BlogSystem\BlogSystem\ and remove all the code present in it and type in the following code in it.

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^blog/', include('blogapp.urls')),

In the above code we are telling django to route the url http://localhost:8000/blog to blogapp\ where we have defined a view for the specified url.

Now we have successfully routed our url now lets define function called index which we have defined previously in our in blogapp/, now delete all the code present in blogapp/ and type in the below code into it.

from django.http import HttpResponse

def index(request):
    return HttpResponse("<h1>This will be the blog</h1>")

The above index function will send a simple HttpRequest defined inside it to the server and will display the output. now open your django server by typing the following command in to the terminal(do not forget to locate your project directory into the terminal)

python runserver

Now go to the following url http://localhost:8000/blog and you will see result as shown below.

blogging system using django

Now in plca of the above text we will show the headings and description of our articles, so go to the following file BlogSystem/blogapp/ and modify the code as shown below

from django.http import HttpResponse

from .models import Article #importing Article model from

def index(request):
    all_data = Article.objects.all()  #fetching data into an object from model Article
    html = ''
    for data in all_data:  #looping through the entire Article model
        url = data.slug
        html += '<h1>'+data.title+'</h1><br /><a href="'+url+'/">Read more</a>'
    return HttpResponse(html)

def show_article(request,slug):
    all_data = Article.objects.filter(slug=slug)  #fetching data into an object from model Article where slug = the slug variable
    html = ''
    for data in all_data:  #looping through the entire Article model
        url = data.slug
        html += '<h1>'+data.title+'</h1><br /><p>'+data.content+'</p>'
    return HttpResponse(html)

In the above code we have comment out some important line and what these lines are actually doing, For function index first we are making an object called all_data and fetching all data from the database using Article.objects.all() which will fetch all the database and then using the for loop we are displaying the data using the object which we created.

In show_article function we are passing an argument called slug from the URL pattern which we defined previously, the function Article.objects.filter() will filter the result for those article having slug field equal to the slug value passed from the URL.

Now if you have followed all the steps properly you will have something as shown below.

Blogging system using django

Thats it for now, in the next tutorial we will design a template and a UI for our blogging System and will finalise our project.

Thank you for reading.

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

Also, don't forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.

If You Love this article, You Should Consider:

  • Like us on Facebook
  • Follow us on Instagram
  • Follow us on Twitter
  • Subscribe to our Newsletter.
  • Let us know your suggestions and queries in the comments below.

Thank you for your Love and Support

Share your thoughts