This article is part 5 of 14 in the series Python Django Tutorial

Last Updated: Thursday 12th December 2013

In the previous article Activate Admin Application for Your Python Django Website, we learned how to activate the built-in Admin Application from Django in your website. In this article, we are going to write simple views for your website.

What is a view?

In Django, a view is an endpoint that can be accessed by any client to retrieve data over the wire. For example, some views serve data rendered with a template to a web server that in turn serves the HTML page to a client. While other views serve JSON data to a web server and these views become part of a RESTful API.

In Django, views are simple Python functions or methods which return data to a front-end web server. When a request comes in, Django chooses a view by examining the request's URL after the domain name. In order to call the correct view when a request is made, you need to insert a list of URL patterns into myblog/ A URL pattern is a regular expression based string which specifies the generic form of a view's URL. For example, r'^posts/(?P\d{4})/$' matches all URLs whose parts after the domain name follow the pattern of a string 'posts' followed by a four-digits number.

Views for Our Django Website

In our blog website, we will write the following views:

  • Post "index" view that displays a list of most recent posts or popular posts in the blog, just like the home page of Python Central
  • Post "detail" view that displays the details of a Post, which is similar to each individual article page on Python Central

In the "index" view, a user should be able to see a list of latest Posts made by others. In the "detail" view, a user should be able to see the content of a Post as well as a list of Comments made by others.

Index View

Let's write your first view "index" that shows a list of most recent Posts. Create a file myblog/ with the following content:

And modify the file myblog/

Now you can access in your browser to view the content returned by myblog.views.index():
Django Home Page Plain

Since the return value of myblog.views.index is a string "This page shows a list of most recent posts.", it shows up on this page. Next, we are going to modify the function myblog.views.index to return a list of Posts that are posted no earlier than two days ago:

Now you can refresh to see that no post is made less than two days ago:
Django Home Page Zero Post

Let's go ahead and add a Post using the Admin interface at the Admin Site:

Django Admin Add a Post

Click "Save" and the post admin page should show that a new post is added and there're three posts total in the database now:

Django Admin Post List Page

Now you can refresh the Home Page to see that one Post is returned:

Django Home Page with One Post

Django Views Templates

Obviously, only displaying "Post object" is not very helpful to the users and we need a more informative page to show a list of Posts. This is when a template would be helpful to turn an useless page like the current one into a presentable one.

Create a new file myblog/templates/index.html with the following content:

And modify the file myblog/ to use the Django template system to render the response of

Now you can refresh the Home Page again to see that one Post is rendered using the template in myblog/templates/index.html:

Django Home Page with Template

Although the current code of the view index() works, it's a bit longer than necessary. You can shorten the code using a Django shortcut render:

Django's Detail View

Similar to the index view, the detail view shows a page that presents detailed information about a Post at URLs like /post/1/detail.html, where 1 is the id of a Post.

Similar to the index view, you need to write the URL pattern before writing the detail view. Modify the file myblog/ in the following way:

Then you can implement the view function in myblog/

Then we need to add a new template file at myblog/templates/post/detail.html:

Now you can access the URL Post Detail:

Django Post Detail Page

If you access a Post whose id is not in the database, our detail view will show a HTTP 404 error page:

Django Post Detail 404 Error Page


In this article, we wrote our first two views. The index view shows a list of posts that are created by users less than two days ago and the detail view presents the detailed content of a Post. Generally speaking, the process of writing a view involves:

  • Write URL patterns for the view in myblog/
  • Write the actual view's code in myblog/
  • Write a template file in the directory myblog/templates/
To Practice: Try this interactive course on the basics of Lists, Functions, Packages and NumPy in Python.

About The Author

  • ajay

    {{ post.content }}
    should be
    {{ post.content }}


    return render(request, ‘post/detail.html’, {‘post’: post})
    should be
    return render(request, ‘detail.html’, {‘post’: post})

    it worked for me …pls correct this if anyone is facing the page not found issue…
    Also pls correct me if i’m wrong!! 🙂

  • ajay

    a href=”/post/{{ }}/”>{{ post.content }}
    should be
    a href=”/post/{{ }}/detail.html”>{{ post.content }}


    return render(request, ‘post/detail.html’, {‘post’: post})
    should be
    return render(request, ‘detail.html’, {‘post’: post})

    it worked for me …pls correct this if anyone is facing the page not found issue…
    Also pls correct me if i’m wrong!! 🙂

  • Nabot Natanael

    Hi, thank you for the insightful blog. I currently facing a challenge to work with my views in django. I have the following code in my views:
    from django.shortcuts import render
    from django.contrib.auth.decorators import login_required
    from django.http import HttpResponse
    from read_data import print_data

    # Create your views here.
    # this login required decorator is to not allow to any
    # view without authenticating
    def home(request):
    return render(request, “home.html”)

    def nata(request):
    return render(request, “nata.html”)
    # return HttpResponse(“nata.html”)

    def temp(request):
    data = print_data()
    return HttpResponse(“” + data + “”)

    I would like to add the def temp(request) function to open in a bootstrap modal on a different page could anyone assist me.