调用 Pyramid 中的另一个视图
我的目标:在 Pyramid 中,调用另一个视图可调用对象,并在不知道有关该视图可调用对象的任何详细信息的情况下获取 Response 对象。
在我的 Pyramid 应用程序中,假设我有一个使用 view_config 装饰器定义的视图“foo”:
@view_config(route_name="foo",
renderer="foo.jinja2")
def foo_view(request):
return {"whereami" : "foo!"}
现在假设我想将“bar”路由到暂时执行相同操作的视图,因此它在内部调用 foo_view
并返回其响应:
@view_config(route_name="bar")
def bar_view(request):
return foo_view(request)
...但是等等!这是行不通的,因为 foo_view
不返回 Response
,而是它的渲染器。
因此,这将起作用:
@view_config(route_name="bar",
renderer="foo.jinja2")
def bar_view(request):
return foo_view(request)
因为它将应用与 foo_view 相同的渲染器。但这很糟糕,因为我现在必须通过复制渲染器值并必须知道被调用视图的渲染器来重复自己。
因此,我希望 Pyramid 中有一些可用的函数,允许调用另一个可调用视图并返回一个 Response 对象,而无需知道或关心它是如何渲染的:
@view_config(route_name="bar")
def bar_view(request):
response = some_function_that_renders_a_view_callable(foo_view, request)
return response
some_function_that_renders_a_view_callable 会怎样?是吗?
pyramid.views.render_view
似乎是按名称搜索视图;我不想说出我的观点。
(注意:返回 HTTPFound 导致客户端重定向到目标路由是我试图避免的。我想“内部”重定向)。
My goal: In Pyramid, to call another view-callable, and to get a Response
object back without knowing any details about that view-callable.
In my Pyramid application, say I have a view "foo" which is defined using a view_config decorator:
@view_config(route_name="foo",
renderer="foo.jinja2")
def foo_view(request):
return {"whereami" : "foo!"}
Now say that I want to route "bar" to a view that does the same thing for the time being, so it internally calls foo_view
and returns its Response:
@view_config(route_name="bar")
def bar_view(request):
return foo_view(request)
...but wait! That doesn't work, since foo_view
doesn't return a Response
, its renderer does.
So, this will work:
@view_config(route_name="bar",
renderer="foo.jinja2")
def bar_view(request):
return foo_view(request)
as it will apply the same renderer as foo_view
did. But this is bad, as I now must repeat myself by copying the renderer value AND having to know the renderer of the view being called.
So, I am going to hope that there is some function available in Pyramid that allows calling another view-callable and getting a Response
object back without knowing or caring how it was rendered:
@view_config(route_name="bar")
def bar_view(request):
response = some_function_that_renders_a_view_callable(foo_view, request)
return response
What would some_function_that_renders_a_view_callable
be?
pyramid.views.render_view
appears to search for a view by name; I don't want to give my views names.
(Note: Returning HTTPFound to cause the client to redirect to the target route is what I am trying avoid. I want to "internally" redirect).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的。有一些问题
请求属性这就是为什么您不应该从视图中调用视图作为函数,除非您知道自己在做什么
金字塔创建者为服务器端重定向做了很棒的工具 - http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/subrequest.html
Yep. There is some concerns
Thats why you should not call view from view as function, unless you know what you doing
Pyramid creators did awesome tool for server side redirect - http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/subrequest.html
您可以使用
request.invoke_subrequest
:
You can invoking a view with using
request.invoke_subrequest
:我也在为此苦苦挣扎。我有一个使用 render_to_response 方法 的解决方案,尽管我确信有一种“更正确”的方法来做到这一点。然而,在有人发布之前,我是这样处理的:(
金字塔 1.3)
这需要使用渲染器,但是通过在原始视图的返回值中声明该渲染器,您可以在另一个视图中检索它,而无需知道它是什么。我怀疑这样做的需要并不容易找到,因为还有其他更好的方法来完成此解决方案解决的任务。
另一个缺点是它依赖于可调用视图的直接导入。如果能直接通过路由查找就好了。
I was struggling with this as well. I have a solution using the render_to_response method, though I'm sure there's a "more correct" way to do it. Until someone posts it, however, here is how I handled this:
(Pyramid 1.3)
This requires a renderer to be used, but by declaring that renderer in the original view's return values, you can retrieve it in another view without knowing what it is. I'm suspecting the need to do this isn't easily findable because there's other, better methods for accomplishing tasks solved by this solution.
Another shortcoming is that it relies on direct import of the view callable. It would be nice if it could be looked up directly by route.
金字塔文档 here 表示将
name
关键字参数保留在view_config
之外将导致视图由函数本身(而不是字符串)注册:因此,在您的情况下,您应该能够使用
pyramid.view.render_view
或pyramid.view.render_view_to_response
code> 直接引用foo_view
:更新:
是的,你说得对,传递视图函数不起作用。
这很有趣,但是采用示例代码并将
route_name
应用于配置不适合我。但是,在下面的示例中,只需为视图指定一个
name
即可设置路由 url并为视图命名。通过这种方式,
render_view_to_response
就如宣传的那样工作了。命名,您的观点可能不是您想要的,但此配置完成了与您相同的事情
示例代码,无需添加配置。
The Pyramid documentation here indicates that leaving the
name
key word argument out ofview_config
will cause the view to be registered by the function itself (rather than a string):So, in your case you should be able to use
pyramid.view.render_view
orpyramid.view.render_view_to_response
referencingfoo_view
directly:Update:
Yep, your right, passing the view function does not work.
It's interesting, but taking your example code and applying the
route_name
to the configdid not work for me. However, the following example, just giving the view a
name
sets the route urland gives the view a name. In this fashion
render_view_to_response
works as advertised. Naming,your views may not be what you want, but this configuration accomplishes the same thing as your
example code without added configuration.
不是您要求的精确解决方案,而是您描述的问题的解决方案:
创建一个视图类,其中 foo 和 bar 都是方法。然后 bar 可以调用 self.foo()
通用的 view_configuration,比如可以将模板名称应用到类中,然后可以只用视图名称来装饰每个方法。
简而言之,如果我正确理解问题,以下内容应该可以满足您的需求。
Not the precise solution you asked for, but a solution to the problem you describe:
Create a view class, of which both foo and bar are methods. Then bar can call self.foo()
Common view_configuration, such as the template name can be applied to the class, and then you can decorate each method with just the view name.
In short, the following should meet your needs, if I understand the problem correctly.
你不能做这样的事情吗:
can't you do something like that: