在 Django 中手动调用基于类的通用视图
我目前正在尝试从另一个基于类的通用视图中调用基于类的通用视图,但似乎无法正确执行此操作。
我尝试过的方法:
result = CategoryTypes.as_view() # The same way you put it in the urlconf
print result
打印:
CategoryTypes.as_view()(self.request)
# &
CategoryTypes().dispatch(self.request)
Tracebacks:
ContentNotRenderedError at /crm/categories/company/ 必须先呈现响应内容,然后才能访问它。 code>
result = CategoryTypes().__init__()
print result
打印: None
我如何从另一个视图调用它?我认真尝试了类中的每一个方法以及我能想到的调用方式。
I'm currently trying to call a class based Generic view from within another class based generic view and cant seem to do it correctly.
Ways I've tried:
result = CategoryTypes.as_view() # The same way you put it in the urlconf
print result
Prints: <function CategoryTypes at 0x92bd924>
CategoryTypes.as_view()(self.request)
# &
CategoryTypes().dispatch(self.request)
Tracebacks:
ContentNotRenderedError at /crm/categories/company/ The response content must be rendered before it can be accessed.
result = CategoryTypes().__init__()
print result
Prints: None
How do I call this from another view? I've seriously tried every method in the class and way of calling it I can think of.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一种方法 -
CategoryTypes.as_view()(self.request)
- 是正确的。问题是,如果您的视图返回TemplateResponse
,则不会自动调用其render
方法。因此,如果您需要访问响应的内容,请先调用
render()
。The first way --
CategoryTypes.as_view()(self.request)
-- is right. The problem is that if your view returns aTemplateResponse
, itsrender
method isn't called automatically.So if you need to access the content of the response, call
render()
on it first.或者您可以通过
result.rendered_content
直接访问内容。在执行此操作之前,请确保在传递到视图之前将会话设置到请求中:Or you can directly access just content via
result.rendered_content
. Before making this be sure you will set session into your request before passing into a view: