--
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.
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}