Flask Url_For() with Multiple Parameters

Flask Url_For() with Multiple Parameters

The Problem:

I have an a input button in a form that when its submitted should redirect two parameters , search_val and i, to a more_results() function, (listed below), but I get a type error when wsgi builds.

The error is: TypeError: more_results() takes exactly 2 arguments (1 given)

html:

 <form action="{{ url_for('more_results', past_val=search_val, ind=i ) }}" method=post>
    <input id='next_hutch' type=submit value="Get the next Hunch!" name='action'>
 </form>

flask function:

@app.route('/results/more_<past_val>_hunches', methods=['POST'])
def more_results(past_val, ind):
    
    if request.form["action"] == "Get the next Hunch!":
        ind += 1 
        queried_resturants = hf.find_lunch(past_val) #method to generate a list
        queried_resturants = queried_resturants[ind]
        return render_template(
                               'show_entries.html', 
                                queried_resturants=queried_resturants, 
                                search_val=past_val,
                                i=ind 
                               )

Any idea on how to get past the build error?

What I've tried:

Creating link to an url of Flask app in jinja2 template

for using multiple paramters with url_for()

Build error with variables and url_for in Flask

similar build erros

As side note, the purpose of the function is to iterate through a list when someone hits a "next page" button. I'm passing the variable i so I can have a reference to keep incrementing through the list. Is there a flask / jinja 2 method that would work better? I've looked into the cycling_list feature but it doesn't seem to able to be used to render a page and then re-render it with cycling_list.next().

2 Answers

It's also possible to create routes that support variable number of arguments, by specifying default values to some of the arguments:

@app.route('/foo/<int:a>')
@app.route('/foo/<int:a>/<int:b>')
@app.route('/foo/<int:a>/<int:b>/<int:c>')
def test(a, b=None, c=None):
   pass
2

Your route doesn't specify how to fill in more than just the one past_val arg. Flask can't magically create a URL that will pass two arguments if you don't give it a two-argument pattern.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.