手动调用基于类的通用视图 [英] Manually calling a class based generic view
问题描述
我正在尝试从另一个基于类的通用视图中调用基于类的通用视图,似乎无法正确执行。
我尝试过的方法:
result = CategoryTypes.as_view()#同样的方式将其放在urlconf
打印结果
打印:< function类型类型在0x92bd924>
/ p>
CategoryTypes.as_view()(self.request)
#&
CategoryTypes()。dispatch(self.request)
追溯:
/ crm / categories / company /
p>
中的ContentNotRenderedError必须在响应内容被访问之前呈现。
result = CategoryTypes().__ init __()
pre>
打印结果
打印:
无
从另一个角度来看这个?我已经认真地尝试过每一个类的方法和调用它的方法我可以想到。
解决方案第一种方法 -
CategoryTypes.as_view()(self.request)
- 是对的问题是,如果您的视图返回一个TemplateResponse
,则不会自动调用其render
方法。
所以如果你需要访问响应的内容,首先调用
render()
。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 on the class and way of calling it I can think of.
解决方案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.这篇关于手动调用基于类的通用视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!