开发者

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
pr开发者_开发技巧int 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.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜