how to add query parameters to the current url in view code?

310 views
Skip to first unread message

Cem Ikta

unread,
Dec 8, 2011, 1:33:20 PM12/8/11
to pylons-...@googlegroups.com
Hi,

How can I add query parameters to the current url in my view code?

I would like to add new parameters to current url if there are no parameters

for example: http://127.0.0.1:6543/countries/list?sort=code

I am using mako + url dispatch.

@view_config(route_name="
countries_list", renderer="countries/list.html")
def list(request):
    """countries list """
    if request.GET.get("sort"):
       sort = request.GET.get("sort")
     else:
       # TODO: add new sort parameter in current url ?
   
    dbsession = DBSession()
    countries = dbsession.query(Country).order_by(sort)

     #TODO:how to return current url with new sort parameter ?
   
    return {"countries": countries}


Thanks.
Cem

Michael Merickel

unread,
Dec 8, 2011, 1:36:34 PM12/8/11
to pylons-...@googlegroups.com
request.route_url() has a _query argument that is documented.

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pylons-discuss/-/ByF2QQn1D30J.
To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.

Cem Ikta

unread,
Dec 9, 2011, 5:34:25 AM12/9/11
to pylons-...@googlegroups.com
I've written below, but it does not work.
is my return is wrong with
return {"countries": countries} ?

request.route_url('countries_list', _query={'sort': sort, 'direction': 'asc'})



Jonathan Vanasco

unread,
Dec 9, 2011, 12:19:09 PM12/9/11
to pylons-discuss
Exactly what do you want to do - and why ?

You're talking about adding a new sort parameter to the "current
url". If you meant that you want to change the URL, then you have two
options:
1. return a pyramid.httpexceptions.HTTPFound . you'd want to do this
before you touch the database or any other code
2. in the html template, have some javascript that will update the
window.location to reflect changes

That being said, I suspect you might be going about this wrong.

I don't understand why you would want to change a URL , or create a
new sort parameter for the URL. In this scenario, as long as there
isn't a sort parameter, you'll always execute that default code. I
see little benefit in redirecting to a new URL if you already have
code to correct for it.

You should also check the user submitted param , to ensure that it is
valid or allowed. i showed this in the 2nd example.

=========

from pyramid.httpexceptions import HTTPFound

@view_config(route_name="countries_list", renderer="countries/
list.html")
def list(request):
"""countries list - with HTTPFound"""


if request.GET.get("sort"):
sort = request.GET.get("sort")
else:

new_url = 'whatever code you want here'
return HTTPFound(location=new_url)


dbsession = DBSession()
countries = dbsession.query(Country).order_by(sort)
#TODO:how to return current url with new sort parameter ?
return {"countries": countries}


=========

@view_config(route_name="countries_list", renderer="countries/
list.html")
def list(request):
"""countries list, condensed, and with some sort of check"""
sort= 'default_sort_order'
if request.GET.get("sort"):
submitted_sort = request.GET.get("sort")
if submitted_sort in [list,of,valid,sorts]:
sort= submitted_sort


dbsession = DBSession()
countries = dbsession.query(Country).order_by(sort)

return {"countries": countries}

Cem Ikta

unread,
Dec 13, 2011, 11:51:21 AM12/13/11
to pylons-...@googlegroups.com
@Michael I have not used HTTPFound, now i understand, thanks.

@Jonathan my logic was wrong, thanks for your explanation, it's with javascript easier.
Reply all
Reply to author
Forward
0 new messages