11

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

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

6 Comments

Did you actually test this out? If you see the first snippet in my question does exactly what you are saying (in one line instead of 3). It does not work because the error view requires a simple handler ... unless it was fixed in recent versions of Django your answer will not work.
I am actually using this (with Django 1.7). How I am using is slightly different, since my 500 view yields JSON instead of HTML. That's why I posted the answer like this instead of using TemplateView.
As a side note, I am also using Python 3.4. I see that the original error is raised in Python 2.6 within the wsgiref library. Maybe this is actually a Python issue, rather than Django.
It shouldn't be related to the version of python, just the version of django. Basically the error view passes less parameters in than what the normal view expects.
Which Django version are you using then? Seems like this might be some case of "works on my machine". :P
|
0

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.

1 Comment

Unless you have minimal context that's required to render the views (e.g. loading templates based on the device, having an error report form etc). That does not really constitute an answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.