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/urls.py. 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/views.py with the following content:

And modify the file myblog/urls.py:

Now you can access 127.0.0.1:8000 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 127.0.0.1:8000 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/views.py to use the Django template system to render the response of 127.0.0.1:8000:

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/urls.py in the following way:

Then you can implement the view function in myblog/views.py:

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

Summary

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/urls.py
  • Write the actual view's code in myblog/views.py
  • Write a template file in the directory myblog/templates/

About The Author

  • ajay

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

    and

    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.id }}/”>{{ post.content }}
    should be
    a href=”/post/{{ post.id }}/detail.html”>{{ post.content }}

    and

    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!! :)