开发者

How can I generate an absolute url for a Pylons controller?

I need to generate a URL for use as a callback in an external system in my pylons application. This requires me to provide both the pylons-app-relative controller path (as generated by the url me开发者_开发问答thod:

>>> relative_url = url(controller='my_cont', action='callback', id=generated_id)
>>> print relative_url
/my_cont/callback/1234

However, I need the whole URL; hostname, relative paths (in case of mod_wsgi, where the path might include other parts from server configuration) &c.

How can I get that?


You should ask url() to generate qualified URL like this:

>>> url(controller='rpc/sessions', action='index')
'/rpc/sessions'
>>> url(controller='rpc/sessions', action='index', qualified=True)
'http://localhost/rpc/sessions'


You can use the routes function url_for, which takes a parameter for host:

from routes import url_for
from pylons import request

abs_url = url_for(host=request.host,
                  controller="my_cont",
                  action="callback",
                  id=gen_id)

Of course, this only works if the host is the same as the request's.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜