Django 1.11 404 Page while Debug = True
Without making it difficult , I just want to show a custom 404 rendering with staticfiles.
If you install DEBUG = False
you can use it in urls.py
handler404 = 'app.views.handler404'
But it doesn't have static files. I don't want to install a web server for a simple application.
C DEBUG = True
in url
url(r'^404/$', views.handler400)
does not override the page Page not found (404) .
What's an easy way to achieve rendering like when you type localhost / asdfhjfsda with staticfiles when DEBUG = True?
Thanks in advance...
In django 1.10 docs :
Changed in Django 1.9: Changed page_not_found () signature. The function now takes a second parameter, the exception that threw the error. A useful representation of the exception is also passed in the context of the template.
Look at the definition 'app.views.handler404'
, it might be missing a parameter, and maybe that's why the handler r'^404/$'
doesn't provide you with the correct method call.
source to share
I have a complete solution
My development environment: Windows 7, Python 3.5.2, Django 1.11, WAMP 3.0.6 (Apache 2.4.23, mod_wsgi)
- Suppose you have error_404.html template with static files
- Create the following directory structure ("mysite" is the root folder of the Django project)
mysite \ mysite \ settings.py urls.py views.py static \ error404 \ files \ style.css image.jpg templates \ error404 \ error_404.html
- <i> MySite \ MySite \ settings.py
import os DEBUG = False TEMPLATES = [{ .. 'DIRS': [os.path.join (BASE_DIR, 'templates')], .. }] STATIC_URL = '/ static /' STATIC_ROOT = 'FullPathToYourSite.com/mysite/static/'
- <i> MySite \ MySite \ urls.py
from django.conf.urls import handler404, handler500 from. import views urlpatterns = [..] handler404 = views.error_404 handler500 = views.error_404
- <i> MySite \ MySite \ views.py
from django.shortcuts import render def error_404 (request): return render (request, 'error404 / error_404.html')
- Some Jinjo logic in "error_404.html" (pseudocode)
{% load staticfiles%} ... link type = "text / css" href = "{% static 'error404 / files / style.css'%}" ... img src = "{% static 'error404 / files / image.jpg'%}" ...
source to share