2016-10-11 200 views
1

我试图在我的应用程序中自定义404错误页面。搜索了很多可能的解决方案之后,我创建了一个404.html模板,添加了一个应该处理HTTP错误404并编辑我的urls.py的方法。自定义404 django模板

但我想我正在做一些非常错误的事情。我的日志显示无效的语法错误,我无法解决它。

我views.py:

# HTTP Error 400 
def page_not_found(request): 
    response = render_to_response('404.html',context_instance=RequestContext(request)) 
    response.status_code = 404 

    return response 

而且语法错误:

Traceback (most recent call last): 
File "/.../myenv/lib/python3.4/site-packages/django/core/urlresolvers.py", line 393, in urlconf_module 
return self._urlconf_module 
AttributeError: 'RegexURLResolver' object has no attribute '_urlconf_module' 
... 
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed 
File "/home/dcaled/work/portal-interface/portal/urls.py", line 12 
handler404 = 'views.handler404' 
      ^
SyntaxError: invalid syntax 

有没有人有什么事情的想法? 谢谢。

更新: @Alasdair建议后,我做了一些修改和修复。错误已经停止。

现在,我的urls.py是这样的:

handler404 = 'views.page_not_found' 

urlpatterns = [ 
    url(r'^$', views.home),]  

但我还是访问一个不存在的页面时,没有得到我的自定义404.html。 相反,默认加载页面时,此消息:

未找到

的请求URL/URL/404没有此服务器上找不到。”

而且,我的settings.py:

DEBUG = False 
ALLOWED_HOSTS = ['*'] 
TEMPLATE_DIRS = (os.path.join(BASE_DIR , 'portal/templates'),) 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [], 
     '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', 
      ], 
     }, 
    }, 
] 

我的项目树:

├── fb_dev 
│   ├── __init__.py 
│   ├── __pycache__ 
│   ├── settings.py 
│   ├── urls.py 
│   └── wsgi.py 
├── manage.py 
│ 
└── portal 
   ├── admin.py 
   ├── forms.py 
   ├── json 
   ├── migrations 
   ├── models.py 
   ├── static 
   ├── templates 
   │   ├── 404.html 
   │   └── portal 
   │    ├── base.html 
   │    ├── home.html 
  ├── templatetags 
   ├── urls.py 
   └── views.py 
+0

您正在使用哪个版本的Django?你的'404.html'模板在哪里? – Alasdair

+0

我的版本是1.8.5。我用模板位置更新了问题。 – revy

+0

你有'TEMPLATES'设置吗?你的代码的其他部分是否成功地在'portal/templates'中找到模板?它可能是你的[浏览器](http://stackoverflow.com/questions/2642340/django-404-page-not-showing-up)或你的服务器(例如Apache)劫持404页面。 – Alasdair

回答

2

几个月后,我重新探讨了问题并确定了解决方案。答案如下:

a)我从views.py中删除page_not_found方法。没有必要添加它。 Django自动获取并呈现404.html模板。

b)再一次,没有必要编辑urls.py。所以我删除了这条线:

handler404 = 'views.page_not_found' 

C)此外,还需要编辑settings.py,设置DEBUGFALSE

DEBUG = False 

而且我删除以下行:

TEMPLATE_DIRS = (os.path.join(BASE_DIR , 'portal/templates'),) 

d)最后,我创建了我的portal/templates目录404.html模板。值得注意的是,自定义模板仅在放入此目录时才呈现。

1

handler404外应urlpatterns。如果视图名为page_not_found,那么它应该参考page_not_found,而不是handler404

handler404 = 'views.page_not_found' 

urlpatterns = [ 
    url(r'^$', views.home), 
] 

但是,就你而言,你根本不需要自定义404处理程序。完全删除handler404行,默认page_not_found视图将呈现404.html模板。

+0

是的,你是对的。我做了这些更改,但仍未显示自定义的404页面。它只显示“未找到 在此服务器上找不到请求的URL/url/404”。任何其他想法? – revy

+0

实际上,DEBUG是错误的。 – revy