Django Tutorial: Custom 404 and 500 Views

Windows 7
Python 2.7.3
Django 1.5
python manage.py runningerver

I am following the tutorial available at https://docs.djangoproject.com/en/1.5/intro/tutorial03/ '

I got to "Write 404 (page not found)" before everything got weird.

I tried to figure out how to create a custom view 404. However, I'm not sure:

a. Where should the 404.html user file be located? Whether it should be in the project directory tree or in the Django directory tree. The project directory tree looks like this:

1> mysite 2> - mysite 3> - polls 4> - templates 5> - polls 6> - templates 7> - admin 

Currently 404.html - @ 6>

Q. What changes should be made where? I understand that handler404 and handler500 need to be installed in the root url. I have url.py residing @ 2> and 3> (see Tree above). Assuming 2> is the right place, I assume the syntax is as follows:

 handler404 = 'templates.404' handler500 = 'templates.500' 

What else needs to be established where?

+6
source share
1 answer

As I can see, you have a small mistake, but do not worry.

Look, try the following:

 handler404 = 'mysite.views.error404' 

Then in mysite (second mysite) view.py is created if you don't have one. In this view.py put:

 from django.shortcuts import render def error404(request): return render(request,'404.html') 

and all! In the templates, create this file and do something nice!

Remember that it only works in production (DEBUG=False) otherwise django uses tracing. The same goes for 505, and you can probably try handler404 = 'polls.views.see404' and put it in views.py, but, you know, it tastes different hehe.

Now try something else like: comment in urls.py

#handler404 = 'mysite.views.error404'

and delete also in views.py def.

A simple 404.html is created in the index of your template file. Start the server in production and produce a 404 error, what happened?

+7
source

All Articles