16

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Samuel Hapak
  • 6,950
  • 3
  • 35
  • 58

2 Answers2

34

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

request.route_url('idea', idea='great', _query={'sort':'asc'})
Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
Samuel Hapak
  • 6,950
  • 3
  • 35
  • 58
5

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.

labreuer
  • 268
  • 2
  • 9
  • 1
    I'm using mako and _query { ... } worked just fine, perhaps you can tell us why it won't work? – CrackSmoker9000 Oct 10 '14 at 21:10
  • @SSIgnatzSchönborn: Did you enclose your `_query { ... }` with `${}`? It seemed that Mako templates would not do nested braces. Perhaps a new version has remedied that? I could try a repro if necessary, although I haven't touched Mako in a while. – labreuer Oct 14 '14 at 18:27