Django handler500作为基于类的视图 [英] Django handler500 as a Class Based View

查看:194
本文介绍了Django handler500作为基于类的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这不工作

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.

我发现这个说明你描述的是你自己在脚下拍摄自己的类视图,为什么会这样?

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?

编辑:我已经结束了使用这个。 ..但我仍然希望有人会告诉我如何获得原始的衬衫或类似的工作

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 1.7中使用Python 3.4):

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



urls.py



urls.py

from .views import Custom500View

handler500 = Custom500View.as_view()

这篇关于Django handler500作为基于类的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆