gpt4 book ai didi

python - 使用 Flask 将多个按钮点击附加到列表中

转载 作者:太空宇宙 更新时间:2023-11-03 20:31:54 25 4
gpt4 key购买 nike

第一次发帖。我很感激任何帮助。我正在获取项目列表并使用 for 循环将它们显示在页面上。每个项目都是一个按钮而不是超链接。我试图做到这一点,以便用户可以单击多个按钮作为“选择”,并将每个按钮的值附加到列表中以供进一步处理。到目前为止,单击时没有任何反应。我尝试转到我创建的测试页面来查看结果,但收到了 404 错误。

在我的模板中,我使用此 For 循环来获取页面上的整个列表。每个列表项的文本都可以像按钮一样单击。但我只是不确定从这里该去哪里。

<form action="/choices" method="post">

{% for i in range(0, toplen) %}

<button class="choice-button" type="submit" name="{{ top[i] }}"
value="{{ request.form.choice }}">{{ top[i] }}</button>
<br>

{%endfor%}

</form>

这是我在 Flask 中的内容:

@app.route('/choices', methods=["POST"])
def choices():
choice_list = []
if request.method == "POST":
choice = request.form.choice
choice_list.append(choice)
return render_template("choices.html", choice_list=choice_list)
else:
return render_template("restaurant_list.html")

现在我只是想让列表显示在choices.html页面上,但我只是得到了404。我真正想做的是让用户单击尽可能多的按钮,并且将结果实时显示在同一页面上。就像对每个选择的确认。

抱歉,如果这没有意义。如果需要更多信息,我可以提供。

谢谢!

最佳答案

使用request.form['choice']

另外请注意,如果您需要此路由来处理 GET 请求,则需要添加 methods=['GET', 'POST'] 因为现在该路由仅处理POST 请求。

关于python - 使用 Flask 将多个按钮点击附加到列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57446401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com