2013-10-03 21 views
6

我已經嘗試使用https://docs.djangoproject.com/en/dev/topics/http/views/教程,但仍然獲得標準的404頁面。我想切換到我的自定義視圖我如何重寫Django中的標準handler404,handler403,handler500?

handler404 = 'myview.views.custom_page_not_found' , 

我做到了(使用eclipse)調試,那麼handler404(old value -'django.config.default.views.page_not_found值)更改爲我給(「myview.views.custom_page_not_found」)的新值。但它仍然顯示較舊的404頁面。我已經將settings.py DEBUG改爲False,然後顯示自定義頁面。但它有一些缺點(它不會加載靜態文件和所有,DEBUG = false是不正確的方式),所以我不得不重置爲True。

我是否需要對此進行其他修改?

回答

0

嘗試添加以下內容到主urls.py的底部:

if settings.DEBUG: 
    urlpatterns += patterns('', 
     (r'^404/$', TemplateResponse, {'template': '404.html'})) 

換出404.html你使用適當的模板,我相信404.html是默認的,但。然後用debug = True,你可以測試你的404頁面。

如果你想和調試測試它= TRUE,那麼你需要這個你的主要urls.py的底部,而不是:

#Enable static for runserver with debug false 
from django.conf import settings 
if settings.DEBUG is False: #if DEBUG is True it will be served automatically 
    urlpatterns += patterns('', 
      url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}), 
    ) 

DEBUG = False運行時,不要忘了收集靜態:

python manage.py collectstatic 

希望這會有所幫助,乾杯!