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()
Django Redirects: A Super Simple Example Just call redirect() with a URL in your view. It will return a HttpResponseRedirect class, which you then return from your view. Assuming this is the main urls.py of your Django project, the URL /redirect/ now redirects to /redirect-success/ .
The most significant advantage of the class-based view is inheritance. In the class-based view, you can inherit another class, and it can be modified for the different use cases. It helps you in following the DRY principle. You won't have to write the same code over and over in your boilerplate.
Django provides a generic class-based view for that very purpose, TemplateView .
I think its actually quite simple (in Django 1.7 with 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With