为什么这不起作用
handler500 = TemplateView.as_view(template_name="500.html")
我得到以下异常:
Traceback (most recent call last):
File "/usr/lib/python2.6/wsgiref/handlers.py", line 94, in run
self.finish_response()
File "/usr/lib/python2.6/wsgiref/handlers.py", line 134, in finish_response
for data in self.result:
File "/home/hatem/projects/leadsift_app/.virtualenv/lib/python2.6/site-packages/django/template/response.py", line 117, in __iter__
raise ContentNotRenderedError('The response content must be 'ContentNotRenderedError: The response content must be rendered before it can be iterated over.
我发现这个set of notes描述了你在脚下射击以使用基于类的视图,为什么会这样?
编辑:我最终使用了这个...但我仍然希望有人能告诉我如何获得原始oneliner或类似的工作class Handler500(TemplateView):
template_name = "500.html"
@classmethod
def as_error_view(cls):
v = cls.as_view()
def view(request):
r = v(request)
r.render()
return r
return view
handler500 = Handler500.as_error_view()
答案 0 :(得分:1)
我宁愿在vanilla Django中使用带有静态HTML的库存500模板,然后对代码执行任何操作。这是一个我认为不应该触及的切换。
答案 1 :(得分:1)
我认为它实际上非常简单(在Django 1.7和Python 3.4中):
from django.http import HttpResponse
from django.views.generic.base import View
class Custom500View(View):
def dispatch(self, request, *args, **kwargs):
return HttpResponse('My custom django 500 page')
from .views import Custom500View
handler500 = Custom500View.as_view()
答案 2 :(得分:-2)
你所需要的一切 -
def redirect_500_error(request):
return render_to_response('errors/500.html', {}, context_instance=RequestContext(request))