Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask url_for() with multiple parameters

Tags:

python

html

flask

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().

like image 965
agconti Avatar asked Jul 26 '13 05:07

agconti


People also ask

For what url_for () function is used in flask?

Flask – URL Building The url_for() function is very useful for dynamically building a URL for a specific function. The function accepts the name of a function as first argument, and one or more keyword arguments, each corresponding to the variable part of URL.

How do I add a variable to a flask URL?

To build a URL to a specific function, use the url_for() function. It accepts the name of the function as its first argument and any number of keyword arguments, each corresponding to a variable part of the URL rule. Unknown variable parts are appended to the URL as query parameters.

What is url_for?

The url_for() function generates the URL to a view based on a name and arguments. The name associated with a view is also called the endpoint, and by default it's the same as the name of the view function.


1 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
like image 167
plaes Avatar answered Oct 13 '22 17:10

plaes