Following the tutorial found here exactly, I cannot create a custom 500 or 404 error page. If I do type in a bad url, the page gives me the default error page. Is there anything I should be checking for that would prevent a custom page from showing up?
File directories:
mysite/
mysite/
__init__.py
__init__.pyc
settings.py
settings.pyc
urls.py
urls.pyc
wsgi.py
wsgi.pyc
polls/
templates/
admin/
base_site.html
404.html
500.html
polls/
detail.html
index.html
__init__.py
__init__.pyc
admin.py
admin.pyc
models.py
models.pyc
tests.py
urls.py
urls.pyc
view.py
views.pyc
templates/
manage.py
within mysite/settings.py I have these enabled:
DEBUG = False
TEMPLATE_DEBUG = DEBUG
#....
TEMPLATE_DIRS = (
'C:/Users/Me/Django/mysite/templates',
)
within mysite/polls/urls.py:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
I can post any other code necessary, but what should I be changing to get a custom 500 error page if I use a bad url?
Edit
SOLUTION: I had an additional
TEMPLATE_DIRS
within my settings.py and that was causing the problem
settings.py::::
and just add your 404.html and 500.html pages in templates folder. remove 404.html and 500.html from templates in polls app.
In Django 2.* you can use this construction in views.py
In settings.py
In urls.py
Usually i creating default_app and handle site-wide errors, context processors in it.
As one single line (for 404 generic page):
Try moving your error templates to
.../Django/mysite/templates/
?I'm note sure about this one, but i think these need to be "global" to the website.
Official answer:
Here is the link to the official documentation on how to set up custom error views:
https://docs.djangoproject.com/en/stable/topics/http/views/#customizing-error-views
It says to add lines like these in your URLconf (setting them anywhere else will have no effect):
You can also customise the CSRF error view by modifying the setting
CSRF_FAILURE_VIEW
.Default error handlers:
It's worth reading the documentation of the default error handlers,
page_not_found
,server_error
,permission_denied
andbad_request
. By default, they use these templates if they can find them, respectively:404.html
,500.html
,403.html
, and400.html
.So if all you want to do is make pretty error pages, just create those files in a
TEMPLATE_DIRS
directory, you don't need to edit URLConf at all. Read the documentation to see which context variables are available.In Django 1.10 and later, the default CSRF error view uses the template
403_csrf.html
.Gotcha:
Don't forget that
DEBUG
must be set to False for these to work, otherwise, the normal debug handlers will be used.From the page you referenced:
So I believe you need to add something like this to your urls.py:
and similar for handler500.