Showing 404 errors In Django

Visit a category page which doesn't exists for example, http://127.0.0.1:8000/category/cat_not_exists/. You should see DoesNotExist exeception like this:

doesnotexist-at-category-cat_not_exists.png

As there is not such category in the database the URL http://127.0.0.1:8000/blog/category/cat_not_exists/ is completely invalid. In production (i.e when DEBUG=True), instead of throwing a DoesNotExist exception Django will show 500 Internal Server Error.

From the point of view of Search Engine Optimization (SEO), it would be much better to show a HTTP 404 error for non existent page instead of showing Internal Server Error/DoesNotExist exception.

The same problem exists in tag page, author page and post detail page. Visit any tag or post detail page which doesn't exists like http://127.0.0.1:8000/tag/tag_not_exists/ or http://127.0.0.1:8000/97712/ and see it yourself.

Showing a HTTP 404 page #

Django provides two ways to show 404 error.

  1. HttpResponseNotFound class
  2. Http404 exception

Let's start with Http404 exception.

HttpResponseNotFound class #

HttpResponseNotFound is an extension of HttpResponse class. It works just like HttpResponse but instead of returning a 200 status code , it returns 404 (which means page not found). To see how it works, open views.py and modify the post_detail() view as follows:

from django.http import HttpResponse, HttpResponseNotFound
...

# view function to display a single post
def post_detail(request, pk):
    try:
        post = Post.objects.get(pk=pk)
    except Post.DoesNotExist:
        return HttpResponseNotFound("Page not found")

    return render(request, 'blog/post_detail.html', {'post': post})
...

Here we are wrapping the problematic code in try and except block. Now, If get() method throws a DoesNotExist exception then instead of displaying an error page, Django will show a 404 error page with "Page not found" response. Save the changes and visit http://127.0.0.1:8000/99999/, you should get a response like this:

404-error-VQCMHN.png

Http404 exception #

Another way to show a 404 error page is to raise Http404 exception. Notice that unlike HttpResponseNotFound class, Http404 is an exception. Http404 uses a default 404 page which Django provides. To use it just raise Http404 exception in your code like this:

raise Http404

You can also provide an error message while raising Http404 expcetion.

raise Http404("Some error message")

Open views.py file and amend post_detail() view as follows:

from django.http import HttpResponse, HttpResponseNotFound, Http404
...

# view function to display a single post
def post_detail(request, pk):
    try:
        post = Post.objects.get(pk=pk)
    except Post.DoesNotExist:
        raise Http404("Post not found")

    return render(request, 'blog/post_detail.html', {'post': post})
...

Save the file and visit http://127.0.0.1:8000/blog/99999/, you will see the following 404 error page.

404-error-using-http404-exception.png

We are currently in development mode. In production (i.e when DEBUG=False) Django will show 404 page which looks like this:

404-error-in-production.png

get_object_or_404() method #

Most of the time our views function goes like this:

  1. Code try and except block.
  2. Query the database in try block.
  3. If exception is thrown, catch the exception in the except block and show a 404 page.

This pattern is so common that Django a provides a shortcurt method called get_object_or_404(). Here is the syntax of get_object_or_404().

Syntax: get_object_or_404(klass, **kwargs)

klass can be a model, a manager or a Queryset object.

**kwargs represents all keyword arguments as well as lookup parameters that we have been using with the get() and filter() method.

On success, it returns a single object of the given model, if it can't find any records then it raises a Http404 exception.

Internally this method calls get() method of objects manager, so you must always use this method to get a single record.

To use get_object_or_404() first import it from django.shortcuts using the following code.

from django.shortcuts import get_object_or_404

The following examples shows how to use get_object_or_404() method with models, queryset and managers. It also shows that when a matching record is not found, get_object_or_404() raises Http404 exception.

Example 1: where klass is model

>>>
>>> from django.shortcuts import get_object_or_404
>>> from blog.models import Category, Post, Tag, Author
>>> post = get_object_or_404(Post, pk=1)
>>>
>>> post
<Post: Post 1>
>>>
>>>
>>> post = get_object_or_404(Post, pk=990)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\K\TGDB\env\lib\site-packages\django\shortcuts.py", line 93, in
get_object_or_404
    raise Http404('No %s matches the given query.' % queryset.model._meta.object
_name)
django.http.response.Http404: No Post matches the given query.
>>>
>>>

The above code is equivalent to:

>>>
>>> try:
...   post = Post.objects.get(pk=1)
... except Post.DoesNotExist:
...   raise Http404("Post not found")
...
>>>

Example 2: where klass is queryset

>>>
>>> queryset = Post.objects.filter(title__contains="Post 1")
>>> queryset
<QuerySet []>
>>>
>>> post = get_object_or_404(queryset, pk=1)
>>>
>>> post
<Post: Post 1>
>>>
>>>
>>> post = get_object_or_404(queryset, pk=10)   # get_object_or_404() will now raise an exception
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\K\TGDB\env\lib\site-packages\django\shortcuts.py", line 93, in
get_object_or_404
    raise Http404('No %s matches the given query.' % queryset.model._meta.object
_name)
django.http.response.Http404: No Post matches the given query.
>>>
>>>

Example 3: where klass is manager

>>>
>>> c = Category.objects.get(name='java')
>>> c
<Category: java>
>>>
>>> post = get_object_or_404(c.post_set, title__contains="Post 2")
>>>
>>> post
<Post: Post 2>
>>>
>>>
>>> post = get_object_or_404(c.post_set, title__contains="tagged")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\K\TGDB\env\lib\site-packages\django\shortcuts.py", line 93, in
get_object_or_404
    raise Http404('No %s matches the given query.' % queryset.model._meta.object
_name)
django.http.response.Http404: No Post matches the given query.
>>>
>>>

Lets update our post_detail() view to use get_object_or_404() method.

TGDB/django_project/blog/views.py

from django.shortcuts import redirect, Http404, get_object_or_404
...

# view function to display a single post
def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})

get_list_or_404() method #

The get_list_or_404() works just like get_object_or_404() method, but instead of returning a single result, it returns a queryset object. If no matching results found, it raises Http404 exception.

Open views.py file and update post_by_category() and post_by_tag() view functions to use get_list_or_404() method as follows:

TGDB/django_project/blog/views.py

from django.shortcuts import redirect, Http404, get_object_or_404, get_list_or_404.
...


# view function to display post by category
def post_by_category(request, category_slug):
    category = get_object_or_404(Category, slug=category_slug)
    posts = get_list_or_404(Post, category=category)
    context = {
        'category': category,
        'posts': posts
    }
    return render(request, 'blog/post_by_category.html', context)



# view function to display post by tag
def post_by_tag(request, tag_slug):
    tag = get_object_or_404(Tag, slug=tag_slug)
    posts = get_list_or_404(Post, tags=tag)
    context = {
        'tag': tag,
        'posts': posts,
    }
    return render(request, 'blog/post_by_tag.html', context )



# view function to display post by author
def post_by_author(request, name):
    author = get_object_or_404(Author, name=name)
    posts = get_list_or_404(Post, author=author)
    context = {
        'author': author,
        'posts': posts
    }
    return render(request, 'blog/post_by_author.html', context )

After updating views, visit any non-existent category page, tag page or post detail page, you should get a proper HTTP 404 error.

Creating SEO friendly URLs #

As situation stands, our post detail page URLs looks like http://127.0.0.1:8000/6/. Although, it is working fine, the problem is that by looking at the URL nobody can tell what the post is about. It would be good to create URLs which describes something about the post.

For our blog, we will create a URL pattern like the following:

http://example.com/post-pk/post-slug

Let's implement this URL pattern.

Open urls.py in the blog app and amend post_detail URL pattern as follows:

TGDB/django_project/blog/urls.py

...

urlpatterns = [
    ...
    url(r'^(?P<pk>\d+)/(?P<post_slug>[\w\d-]+)$', views.post_detail, name='post_detail'),
    ...
]

Update post_detail() view function to accept another parameter called post_slug as follows:

TGDB/django_project/blog/views.py

...

# view function to display a single post
def post_detail(request, pk, post_slug):
    post = get_object_or_404(models.Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})

...

At last, we need to modify get_absolute_url() method of the Post model. Open models.py and update get_absolute_url() method of Post models as follows:

TGDB/django_project/blog/models.py

...
class Post(models.Model):
    ...
    def get_absolute_url(self):
        return reverse('post_detail', args=[self.id, self.slug])
...

As we are using get_absolute_url() method to generate links to post detail page in our templates, we don't need to modify anything else. All templates will pick the changes automatically.

To view updated post detail URLs visit http://127.0.0.1:8000/ and click on any post detail page.

updated-url-structure-HFBRTL.png

Listing posts in reverse chronological order #

All pages of our site displays posts in the order in which they are inserted into the database. For usability purpose, it would be much better to display posts in reverse chronological order i.e latest post first, oldest post last.

This change would be very simple. Open views.py in the blog app and update post_list(),
post_by_category(), post_by_tag(), post_by_author() views as follows:

...

# view function to display a list of posts
def post_list(request):
    posts = Post.objects.order_by("-id").all()
    return render(request, 'blog/post_list.html', {'posts': posts})


# view function to display post by category    
def posts_by_category(request, category):
    category = get_object_or_404(Category, slug=category_slug)
    posts = get_list_or_404(Post.objects.order_by("-id"), category=category)
    ...


# view function to display post by tag
def posts_by_tag(request, tag):
    tag = get_object_or_404(Tag, slug=tag_slug)
    posts = get_list_or_404(Post.objects.order_by("-id"), tags=tag)
    ...


# view function to display post by author
def post_by_author(request, username):
    author = get_object_or_404(Author, username=username)
    posts = get_list_or_404(Post.objects.order_by("-id"), author=author)
    ...

From now on, all the pages of our blog will display posts in reverse chronological order.