I Want to Build Your Website


Follow me here and let me show you what I can do!

404 Pages in Django


While searching this very blog, I noticed something very disturbing that I had never taken care of when building it. I had never integrated the dreaded 404 page. That's right, I had never really thought about what a sly user might be up to when on the site.

Lucky for me I didn't have any really "personal" data out there, but imagine if it was one of my customers websites. That would have looked pretty unprofessional. Luckily Django makes it really easy to create such a page.

The first thing that I had to do, was go into my "settings.py" file and change the line that says:

DEBUG = True

to

DEBUG = False

That change in itself is huge, here are some of the reasons to use DEBUG = False in your production environment.

  • All database queries will be saved in memory in DEBUG = True. Can you imagine the amount of memory you'll be saving if you turn this off? Huge.
  • All the sensitive data that Django supplies when your in development mode will no longer show up. You've seen it when your doing development, but do you think that's really something the public needs to see? Nah, me either, so make sure DEBUG = False.
  • No more exception page errors. You know that page that shows up when your app bombs out. Well that has almost all the keys to castle in it, with user names and such, so we really want to make sure that doesn't show up.

With all that said, all that's left for you to do, is create a 404.html template and a 500.html template. Both of these templates need to reside at the root of your template directory. It's a good idea to use inheritance with the 404.html, but not with the 500.html page. You never know what's going on with those 500 error pages or what's throwing the error, so just create something that doesn't inherit from your base.html page.

Once those are created, try going to a url on your site that doesn't exist and see what it does. You'll notice that you no longer have the Django error page.

| More



Comments


No Response Yet

Why don't YOU kick it off by saying something ↓

Comment Form

Content is licensed under a Creative Commons Public Domain License