Designing custom 404 and 500 error pages in Django

Update 1: Please refer this updated article for Django 2.0 and source code.  


It happens very frequently that a visitor on your website typed a wrong URL or the page user is looking for no longer exists.

What do you do to handle such cases. You have three options.

  • Redirect the visitor to home page, silently.
  • Show a boring 404 page and then ask them to click on a link.
  • Create your own funny/awesome/informative custom 404 error page.


In this article we will discuss the third option.


How to create your own custom 404 error page in Django:

A custom 404 error page can serve multiple other purposes apart from just telling the user that the link you visited is not correct.

You can ask user to subscribe or sign-up. Or you may show some funny stuff.


custom 404 error page in django


Default 404 error page in django is quite boring. Also creating a custom 404 page in django is very simple.  

So lets see how to create custom 404 error page in django.

  • In urls.py  file of your project, import handler404  and handler505 .
  • In urls.py  file import views from your app.
  • After urlpatterns assign the views to handler404 and handler500.

    from myapp import views as myapp_views
    from django.conf.urls import handler404, handler500
    
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^myapp/', include('myapp.urls', namespace='myapp')),
    ] 
    
    handler404 = myapp_views.error_404
    handler500 = myapp_views.error_500


  • In settings.py file, set DEBUG=False  and ALLOWED_HOST=["*"] . Custom 404 and 500 pages works only when Debug is set to false and there is appropriate entry in allowed_hosts .
  • Create a new view function in your views file. Return the rendered 404 html file from this view. Make sure name of view function created in this file matches the name used in urls file above.

    from django.shortcuts import render
    
    
    def error_404(request):
            data = {}
            return render(request,'myapp/error_404.html', data)
    
    def error_500(request):
            data = {}
            return render(request,'myapp/error_500.html', data)


  • Now create an HTML template in your app's template directory myapp/templates/myapp/error_404.html . Place whatever content you want to put there. You can create a subscribe form or sign up form.
  • Now type an incorrect url and you will be shown your custom 404 page. In case of any internal server error like syntax error in some template, custom 500 page will be shown.


  Comment in case of any query.

Related Articles:
Python Script 3: Validate, format and Beautify JSON string Using Python
Validating json using python code, format and beautify json file using python, validate json file using python, how to validate, format and beautify json...
Encryption-Decryption in Python Django
How to encrypt and decrypt the content in DJango. Encrypting the critical information in Django App. Encrypting username, email and password in Django...
Adding Robots.txt file to Django Application
Adding robots.txt file in your Django application. Easiest way to add robots.txt file. Django application robots.txt file. Why should you add robots.txt file in your Django Application....
How to upgrade to paid account on PythonAnyWhere
Which is the best server for hosting Django Apps. Best hosting provider for Django Apps. Cheapest Django Hosting. PythonAnyWhere Reviews. Django Hosting....

0 thoughts on 'Designing Custom 404 And 500 Error Pages In Django'
Leave a comment:


*All Fields are mandatory. **Email Id will not be published publicly.


SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.



Recent Posts:





© pythoncircle.com 2018-2019