Without making things difficult, I just want to show a special 404 render with staticfiles.
If you set DEBUG = False
you can use in urls.py
handler404 = 'app.views.handler404'
But it is without staticfiles. I don't want to install a web server for a simple app.
With DEBUG = True
in urls
url(r'^404/$', views.handler400)
is not overriding the default Page not found (404) page.
What is the easy way to achieve a render e.g. when you type localhost/asdfhjfsda with staticfiles when DEBUG=True?
Thanks in advance...
In django 1.10 docs:
Changed in Django 1.9: The signature of page_not_found() changed. The function now accepts a second parameter, the exception that triggered the error. A useful representation of the exception is also passed in the template context.
Have a look at your 'app.views.handler404'
definition, it might miss a parameter, and maybe it's that why the r'^404/$'
handler doesn't provide you with the correct method invocation.
Easiest way to do this post Django 1.9 is in your urls.py
:
from django.views.defaults import page_not_found
url(r'^404/$', page_not_found, {'exception': Exception()})
It wants an exception, give it an exception :)
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 next directory structure ("mysite" - Django project root folder)
mysite\ mysite\ settings.py urls.py views.py static\ error404\ files\ style.css image.jpg templates\ error404\ error_404.html
- 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/'
- mysite\mysite\urls.py
from django.conf.urls import handler404, handler500 from . import views urlpatterns = [..] handler404 = views.error_404 handler500 = views.error_404
- 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' %}" ...
来源:https://stackoverflow.com/questions/44228397/django-1-11-404-page-while-debug-true