Django handler500 as a Class Based View
Asked Answered
G

2

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()
Grannie answered 29/11, 2012 at 19:47 Comment(0)
P
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()
Pendragon answered 27/3, 2015 at 10:20 Comment(6)
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.Grannie
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.Pendragon
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.Pendragon
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.Grannie
Which Django version are you using then? Seems like this might be some case of "works on my machine". :PPendragon
When this was asked it was Django 1.6 ... will test to see if your answer works on 1.7Grannie
A
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.

Accelerando answered 3/12, 2012 at 5:59 Comment(1)
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.Introjection

© 2022 - 2024 — McMap. All rights reserved.