Get Pyramid View callable by it's path (reques

2019-08-03 07:42发布

I am making an app that would translate websocket messages to AJAX requests to the server. Mainly the decision is based on the fact that Pyramid already has a good URL dispatch system and it would be stupid not to use it.

The question is if there is an easy way to dispatch a URL in Pyramid (possibly an instanced Request object) to it's according view callable and get the view callable? Or at least get the output of the view callable related to the request?

I have tried the script from "prequest.py" which basically emulates a whole HTTP client and gives you the response (I have still not managed to get it work, but a look through the sources makes sense anyway) and I wouldn't like to do it that way.

2条回答
时光不老,我们不散
2楼-- · 2019-08-03 08:00

I have managed to do it using Router.invoke_subrequest in the latest version of Pyramid (1.4a1).

This enables all the features related to routing. URL dispatch, parameter passing, tweens.

You can read about it here: http://docs.pylonsproject.org/projects/pyramid/en/latest/api/request.html#pyramid.request.Request.invoke_subrequest

查看更多
Viruses.
3楼-- · 2019-08-03 08:02

You can reuse the code from the pview command to turn a path into a view reference:

from pyramid.scripts.pviews import PViewsCommand

pvcomm = PViewsCommand([])
view = pvcomm._find_view(path, request.registry)

The actual code to do this is a little involved, but the PViewsCommand does it all for us already.

查看更多
登录 后发表回答