手动调用基于类的通用视图 [英] Manually calling a class based generic view

查看:135
本文介绍了手动调用基于类的通用视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从另一个基于类的通用视图中调用基于类的通用视图,似乎无法正确执行。



我尝试过的方法:

  result = CategoryTypes.as_view()#同样的方式将其放在urlconf 
打印结果

打印:< function类型类型在0x92bd924> / p>

  CategoryTypes.as_view()(self.request)
#&
CategoryTypes()。dispatch(self.request)

追溯:



/ crm / categories / company /
中的ContentNotRenderedError必须在响应内容被访问之前呈现。
p>

  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 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.

这篇关于手动调用基于类的通用视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆