Get Pyramid View callable by it's path (request context)

前提是你 提交于 2019-12-11 11:01:38

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/14707400/get-pyramid-view-callable-by-its-path-request-context

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