Home > database >  Missing Argument When Redirecting to URL with Flask/Python
Missing Argument When Redirecting to URL with Flask/Python

Time:02-27

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'

CodePudding user response:

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.

  • Related