Django 1.11 404 Page while Debug=True

≡放荡痞女 提交于 2019-12-06 02:31:41

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 :)

NuzhdinVic

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)

  1. Suppose you have error_404.html template with static files
  2. 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
  1. 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/'
  1. mysite\mysite\urls.py
    from django.conf.urls import handler404, handler500
    from . import views

    urlpatterns = [..]

    handler404 = views.error_404
    handler500 = views.error_404
  1. mysite\mysite\views.py
    from django.shortcuts import render

    def error_404(request):
        return render(request, 'error404/error_404.html')
  1. 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' %}"
    ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!