[英]Manually calling a class based generic view in Django
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>
打印:
<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
印刷品:
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.
我已经认真尝试了课堂上的所有方法以及我能想到的调用方式。
The first way -- CategoryTypes.as_view()(self.request)
-- is right.第一种方式
CategoryTypes.as_view()(self.request)
——是正确的。 The problem is that if your view returns a TemplateResponse
, its render
method isn't called automatically.问题是如果你的视图返回一个
TemplateResponse
,它的render
方法不会被自动调用。
So if you need to access the content of the response, call render()
on it first.因此,如果您需要访问响应的内容,请先在其上调用
render()
。
Or you can directly access just content via result.rendered_content
.或者您可以通过
result.rendered_content
直接访问内容。 Before making this be sure you will set session into your request before passing into a view:在进行此操作之前,请确保您在传递到视图之前将会话设置为您的请求:
self.request.session = {}
CategoryTypes.as_view()(self.request)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.