为什么这不起作用

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描述您正在脚下射击以使用基于类的 View ,这是为什么呢?

编辑:我最终使用了此...但是我仍然希望有人在那里告诉我如何获得原始的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()

最佳答案

我认为它实际上非常简单(在Django 1.7和Python 3.4中):

views.py

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')

urls.py
from .views import Custom500View

handler500 = Custom500View.as_view()

08-08 00:38