18

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

3 Answers 3

38

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
Sign up to request clarification or add additional context in comments.

2 Comments

Is the above answer can be considered as a correct way when dealing with multi-step forms in flask? Such that each step is one argument.
@plaes This does not work for me. It gives 404 if I don't pass arguments b and c
9

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.

Comments

0

try to serialize your kwargs with json.dumps() like this

<form action="{{ url_for('more_results',kwargs=json.dumps({"past_val":search_val,"ind":i} ) }}" method=post>

On the flask side,

@app.route('/results/more_<kwargs>_hunches', methods=['POST'])
def more_results(kwargs):
   kwargs = json.loads(kwargs)
   past_val=kwargs['past_val']
   ind = kwargs['in']

alternatively, you could use flask.session or flask.g to manage these variables with the context of the session or request eg. assign the value to a session['past_val'] = search_val and use the session attributes anywhere including with Jinja template like so {{session['past_val']}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.