【发布时间】:2022-01-13 11:24:24
【问题描述】:
单击相应按钮时,我正在尝试将 for 循环中生成的 jinja 变量传递给后端。当我单击任何按钮时,列表中的第一个值被传递。我试过的代码是
代码
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/search_tag', methods=["GET", "POST"])
def search_tag():
if request.method == 'POST':
print("hi")
tag_x = request.form['rawtext']
print(tag_x)
return render_template('index.html', messg = "SUCCESS")
else:
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
HTML 代码
<!DOCTYPE html>
<html>
<head>
<title>Test App</title>
</head>
<body>
<form method="POST" action="{{ url_for('search_tag')}}">
<div id ="result">
<p style="color:red">
{% for i in ["hello","hi","travel","goal"] %}
<li>{{ i }}
<input type="hidden" id="rawtext" name="rawtext" value="{{ i }}">
<input type="submit" name="submit" value="click" class="btn btn-info">
</li>
{% endfor %}
</p><br>
</div>
</form>
{{ messg }}
</body>
</html>
</body>
</html>
提前致谢
【问题讨论】: