使用 Flask/Python 重定向到 URL 时缺少参数

发布于 2025-01-10 14:29:04 字数 1654 浏览 0 评论 0原文

我在从表单向 Flask 路由传递参数时遇到问题。我是 Flask 的新手,所以如果这很简单,我很抱歉。我整个早上都在重新做和重新编码,但毫无结果。我正在尝试返回一个 HTML 页面,其中包含表单中的用户输入值。我已经成功地使用表单(硬币)中的一个字段完成了此操作,但是当我以完全相同的方式添加第二个变量时,我不断收到“缺少 1 个必需的位置参数”错误。

下面的 Flask 代码

@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)

我正在尝试呈现以下 HTML 模板:

{% extends "index.html" %}

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

仅在 '/new 的 coin_add() 函数中使用“coin”参数时没有任何问题-coin/'路线。但是,当我尝试添加限制和高价变量时,它坚持认为该参数缺失。我什至将“limit”变量打印到控制台以查看它是否存在并且在重定向行之前成功打印出来。我不知道为什么这没有被传递到 coin_added 路由作为限制。

如果我从 coin_added 函数中删除“limit”参数,一切都会正常。我很困惑为什么它说“限制”参数丢失,当它在上面传递时。

错误消息

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

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

却一份温柔 2025-01-17 14:29:04

我认为你的路线缺少限制变量。它应该是这样的 有关

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

如何使用多个参数的更多信息此处

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文