Pyramids route_url with additional query arguments

和自甴很熟 提交于 2019-12-04 16:51:19

问题


In Pyramids framework, functions route_path and route_url are used to generate urls from routes configuration. So, if I have route:

config.add_route('idea', 'ideas/{idea}')

I am able to generate the url for it using

request.route_url('idea', idea="great");

However, sometimes I may want to add additional get parameters to generate url like:

idea/great?sort=asc

How to do this?

I have tried

request.route_url('idea', idea='great', sort='asc')

But that didn't work.


回答1:


You can add additional query arguments to url passing the _query dictionary

request.route_url('idea', idea='great', _query={'sort':'asc'})



回答2:


If you are using Mako templates, _query={...} won't work; instead you need to do:

${request.route_url('idea', idea='great', _query=(('sort', 'asc'),))}

The tuple of 2-tuples works as a dictionary.



来源:https://stackoverflow.com/questions/11989317/pyramids-route-url-with-additional-query-arguments

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