Django自定义404页面无效

Aug*_*toQ 5 python django error-handling http-status-code-404

所以,在我的项目中,我想添加一个自定义的404错误页面,所以我按照我在网上找到的内容,但似乎没有什么对我有用.

这就是我的文件:

settings.py

import os
# Django settings for HogwartsMail project.

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

DEBUG = False
TEMPLATE_DEBUG = DEBUG

# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = ["*"]
Run Code Online (Sandbox Code Playgroud)

urls.py

urlpatterns = patterns('',
    ...
)

handler404 = "HogwartsMail.views.error404"
Run Code Online (Sandbox Code Playgroud)

views.py

def error404(request):
    return render(request,'404.html')
Run Code Online (Sandbox Code Playgroud)

每当我尝试输入一个随机网址时,我得到的服务器错误(500)并没有得到404错误.

正如我所说,我尝试了许多不同的方式,但没有一个真正适合我.

另外,我遇​​到的另一个问题是我加载的页面非常慢,并且它们没有打开样式或图像,所以我认为,当DEBUG = False我需要一切已经在线时.那是对的吗?

She*_*wei 5

DEBUG = False,您可以用于python manage.py runserver --insecure在本地开发期间提供静态文件。

请参阅此答案中的更多信息。


cra*_*ubr 1

def error404(request):
    return HttpResponseNotFound(render_to_string('404.html'))
Run Code Online (Sandbox Code Playgroud)

另一个例子:

def error404(request):
    return render(request,'404.html', status=404)
Run Code Online (Sandbox Code Playgroud)