I'm going through the Django tutorial and am on part 5: Testing. I run into the problem where I'm using the DetailView and ListView "shortcut" views to factor out code (as suggested by the tutorial), but when a 404 page is displayed, a 200 status code is returned instead. Am I doing something wrong? The tutorial says the status code should be 404.
我正在阅读Django教程,并在第5部分:测试。我遇到的问题是我使用DetailView和ListView“快捷方式”视图来分解代码(如教程所示),但是当显示404页面时,将返回200状态代码。难道我做错了什么?该教程说状态代码应该是404。
Thanks!
2 个解决方案
#1
4
You need to define the Http header to have a 404 status.
您需要将Http标头定义为404状态。
return HttpResponse(content=template.render(context), content_type='text/html; charset=utf-8', status=404)
It is important to inform the search engines that the current page is a 404. Spammers sometimes creates lots of urls that could seem that would lead you to some place, but then serves you another content. They frequently make lots of different addresses serve you almost the exact same content. And because it is not user friendly, most SEO guide lines penalize that. So if you have lots of addresses showing the same pseudo-404 content, it could not look good to the crawling systems from the search websites. Because of that you want to make sure that the page you are serving as a custom 404 has a 404 status.
重要的是告知搜索引擎当前页面是404.垃圾邮件发送者有时会创建许多可能会引导您到某个地方的网址,但随后会为您提供另一个内容。他们经常制作许多不同的地址,为您提供几乎完全相同的内容。而且由于它不是用户友好的,大多数SEO指南都会对此进行惩罚。因此,如果您有大量地址显示相同的伪404内容,则搜索网站的爬网系统看起来不太好。因此,您需要确保您作为自定义404使用的页面具有404状态。
If you are trying to make a custom 404 page, here it is a good way to go:
如果您正在尝试制作自定义404页面,这里是一个很好的方法:
Into your application's urls.py add:
进入你的应用程序的urls.py添加:
# Imports
from django.conf.urls.static import static
from django.conf.urls import handler404
from django.conf.urls import patterns, include, url
from yourapplication import views
##
# Handles the URLS calls
urlpatterns = patterns('',
# url(r'^$', include('app.homepage.urls')),
)
handler404 = views.error404
Into your application's views.py add:
进入你的应用程序的views.py添加:
# Imports
from django.shortcuts import render
from django.http import HttpResponse
from django.template import Context, loader
##
# Handle 404 Errors
# @param request WSGIRequest list with all HTTP Request
def error404(request):
# 1. Load models for this view
#from idgsupply.models import My404Method
# 2. Generate Content for this view
template = loader.get_template('404.htm')
context = Context({
'message': 'All: %s' % request,
})
# 3. Return Template for this view + Data
return HttpResponse(content=template.render(context), content_type='text/html; charset=utf-8', status=404)
The secret is in the last line: status=404
秘密在最后一行:status = 404
Hope it helped!
希望它有所帮助!
I look forward to see the community inputs to this approach. =)
我期待社区对这种方法的投入。 =)
#2
0
You can
return HttpResponseNotFound(render_to_string('404.html'))
instead.
#1
4
You need to define the Http header to have a 404 status.
您需要将Http标头定义为404状态。
return HttpResponse(content=template.render(context), content_type='text/html; charset=utf-8', status=404)
It is important to inform the search engines that the current page is a 404. Spammers sometimes creates lots of urls that could seem that would lead you to some place, but then serves you another content. They frequently make lots of different addresses serve you almost the exact same content. And because it is not user friendly, most SEO guide lines penalize that. So if you have lots of addresses showing the same pseudo-404 content, it could not look good to the crawling systems from the search websites. Because of that you want to make sure that the page you are serving as a custom 404 has a 404 status.
重要的是告知搜索引擎当前页面是404.垃圾邮件发送者有时会创建许多可能会引导您到某个地方的网址,但随后会为您提供另一个内容。他们经常制作许多不同的地址,为您提供几乎完全相同的内容。而且由于它不是用户友好的,大多数SEO指南都会对此进行惩罚。因此,如果您有大量地址显示相同的伪404内容,则搜索网站的爬网系统看起来不太好。因此,您需要确保您作为自定义404使用的页面具有404状态。
If you are trying to make a custom 404 page, here it is a good way to go:
如果您正在尝试制作自定义404页面,这里是一个很好的方法:
Into your application's urls.py add:
进入你的应用程序的urls.py添加:
# Imports
from django.conf.urls.static import static
from django.conf.urls import handler404
from django.conf.urls import patterns, include, url
from yourapplication import views
##
# Handles the URLS calls
urlpatterns = patterns('',
# url(r'^$', include('app.homepage.urls')),
)
handler404 = views.error404
Into your application's views.py add:
进入你的应用程序的views.py添加:
# Imports
from django.shortcuts import render
from django.http import HttpResponse
from django.template import Context, loader
##
# Handle 404 Errors
# @param request WSGIRequest list with all HTTP Request
def error404(request):
# 1. Load models for this view
#from idgsupply.models import My404Method
# 2. Generate Content for this view
template = loader.get_template('404.htm')
context = Context({
'message': 'All: %s' % request,
})
# 3. Return Template for this view + Data
return HttpResponse(content=template.render(context), content_type='text/html; charset=utf-8', status=404)
The secret is in the last line: status=404
秘密在最后一行:status = 404
Hope it helped!
希望它有所帮助!
I look forward to see the community inputs to this approach. =)
我期待社区对这种方法的投入。 =)
#2
0
You can
return HttpResponseNotFound(render_to_string('404.html'))
instead.