Manually calling a class based generic view

*爱你&永不变心* 提交于 2019-12-05 09:55:13

问题


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.


回答1:


The first way -- CategoryTypes.as_view()(self.request) -- is right. The problem is that if your view returns a TemplateResponse, its render method isn't called automatically.

So if you need to access the content of the response, call render() on it first.




回答2:


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:

self.request.session = {}
CategoryTypes.as_view()(self.request)


来源:https://stackoverflow.com/questions/7258912/manually-calling-a-class-based-generic-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!