Johan Vergeer Johan Vergeer - 1 month ago 23
Python Question

Django 1.10 404.html template not found

As far as I have seen in the Django documentation, to show a custom 404 page all you have to do is put a 404.html in the root templates directory.

So my project structure is:

django_project
|_ config
| |_ settings.py
| |_ urls.py
|
|_ templates
|_ base.html
|_ index.html
|_ 404.html


In settings.py I have the following settings for the templates:

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ["templates", ]
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'contact.views.contact_info',
],
},
},
]


for the
'DIRS'
parameter I also used
os.path.join(BASE_DIR, "templates")
. This had the exact same outcome.

I also used

TEMPLATE_DIRS = (
os.path.join(BASE_DIR, "templates"),
)


This lead to a deprecation warning.

In urls.py I also did not really do anything special:

from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView

urlpatterns = [
url(r'^$', TemplateView.as_view(template_name="index.html"), name="index"),

url(r'^admin/', admin.site.urls),
]


Template inheritance is working perfectly in other apps, so the templates directory is found.

In settings:
DEBUG=False


When I enter a wrong url I get the Django default 'NOT FOUND' page.

What am I missing here?

Answer

you need override 'handler404' variable, add this to urls.py

from django.conf.urls import handler404

handler404 = 'your_app.views.404'

https://docs.djangoproject.com/en/1.10/topics/http/views/#customizing-error-views

Comments