0

I am having trouble with passing arguments to a Flask route from a Form. I am new to Flask so my apologies if this is something simple. I have been re-doing and re-coding things all morning but to no avail. I am trying to return an HTML page with the user input values from a form. I have done it successfully with one field form the form (coin), but when I add a second variable the exact same way I keep getting the "missing 1 required positional argument" error.

Flask Code Below

@app.route('/addcoin/', methods=['POST', 'GET'])
def addcoin():
    if request.method == "POST":
        coin = request.form['Symbol']
        high_price = request.form['High Price']
        api = request.form['API']
        channel = request.form['Channel']
        # return f"<h1>{coin}{high_price}{api}{channel}</h1>"
        print('High Price is ' + high_price)
        print(coin)
        print(type(high_price))
        limit = float(high_price)
        print(limit)

        return redirect(url_for('coin_added', coin_added=coin, limit=limit))
    else:
        return render_template('addcoin.html')


@app.route('/new-coin/<coin_added>')
def coin_added(coin_added, limit):
    return render_template('new_coin.html', coin=coin_added, high_price=limit)

I am trying to render the below HTML template:

{% extends "index.html" %}

{% block content %}
<h1>{{ coin }} has bee added!</h1>
<h1>{{ high_price }} is the cutoff price!</h1>
{% endblock %}

I have no issues when only using the "coin" argument within the coin_added() function of the '/new-coin/<coin_added>' route. But when I try and add the limit and high price variable it insists that the argument is missing. I am even printing out the "limit" variable to console to see if it exists and it prints out successfully before the redirect line. I am not sure why this is not getting passed to the coin_added route as limit.

If I remove the "limit" argument from the coin_added function everything works fine. I am very confused as to why it is saying the "limit" argument is missing, when it is getting passed in right above this.

Error Message

TypeError: coin_added() missing 1 required positional argument: 'limit'

1 Answer 1

1

I think your route is missing the limit variable. It should be something like this

@app.route('/new-coin/<coin_added>/<limit>')

More information on how to have multiple parameters here.

Sign up to request clarification or add additional context in comments.

3 Comments

That was indeed it! I didn't realize I needed to also specify an additonal parameter in the route URL. Thank you so much!
Would there be a way for me to have the URL dynamically change based on just the <coin_added> argument, but still display both on the HTML page?
I'm not really sure what you are trying to achieve, but you could always just either redirect to a different route or use some sort of pattern matching and display different templates based on that argument.

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.