当前位置: 动力学知识库 > 问答 > 编程问答 >

Create 404 with Django 1.4

问题描述:

I would like to show an 404 error if there's no proper entry in project_name/urls.py.

I read Writing your first Django app but i don't understood how to create a general error - not only for an app.

So i tried this:

  1. TEMPLATE_DIRS = /var/www/first/templates
  2. echo "404 ERROR" > /var/www/first/templates/404.html
  3. in /var/www/first/urls.py:

    from django.http import Http404

    try:

    ...

    url(r'^admin/', include(admin.site.urls)),

    ...

    except:

    raise Http404

After all i still get a 500 error if i call an unknown url.

网友答案:

Django uses the urls.py file to evaluate all the url patterns by order. If you want it to go to a certain page after it evaluated all the regex patterns and found no match, just add something like this as the last item in the urlpatterns tuple:

url('', views.defaultview),

with defaultview possibly being that 404 page.

django.views.defaults.page_not_found is a built-in django view that renders whatever's in templates/404.html.

网友答案:

It works with following in /var/www/first/urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
...,
url(r'^admin/', include(admin.site.urls)),
...,
)
分享给朋友:
您可能感兴趣的文章:
随机阅读: