问题
Why does this not work
handler500 = TemplateView.as_view(template_name="500.html")
I get the following exception:
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.
I found this set of notes that describe that you are shooting yourself in the foot to use class based views there, why is that?
EDIT: I have ended up using this ... but I am still hoping someone out there would tell me how to get the original oneliner or similar working
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()
回答1:
I would rather just use stock 500 templates with static HTML in vanilla Django then do anything with code. This is one toggle I believe should not be touched.
回答2:
I think its actually quite simple (in Django 1.7 with 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()
来源:https://stackoverflow.com/questions/13633508/django-handler500-as-a-class-based-view