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我需要一切已经在线时.那是对的吗?
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)
| 归档时间: |
|
| 查看次数: |
7937 次 |
| 最近记录: |