0

Some of my endpoints of my flask api use values from an html form to determine what data to return. I'd like to pass the parameters that I use to generate the data I'm returning back to the URL rendered in the browser. Is there a way to do that with flask?

Example:

http://example.com/search

On the page, the user types the word "duck" into a search box, hits go, and in addition to the results, Flask returns url showing all the parameters I passed into the api underneath the application... e.g:

http://example.com/search?&search_term=duck&page=0&per_page=20
kelorek
  • 6,042
  • 6
  • 29
  • 32

1 Answers1

3

You can use url_for to generate the url:

from flask import redirect, url_for, request
...
# duck = request.form.get('search_term')
return redirect(url_for('search', search_term=duck, page=0, per_page=20))
...
atupal
  • 16,404
  • 5
  • 31
  • 42
  • Currently I'm ending the method with render_template() which has a form, and that form POSTs to a url_for(). I don't seem to be able to pass parameters into the url_for() in the jinja template. Is that expected? – kelorek Feb 16 '14 at 08:46
  • @kelorek You can use `url_for` in jinja too, see http://stackoverflow.com/questions/9023488/build-error-with-variables-and-url-for-in-flask/15838522#15838522 and http://stackoverflow.com/questions/11124940/creating-link-to-an-url-of-flask-app-in-jinja2-template – atupal Feb 16 '14 at 08:56