![](/img/trans.png)
[英]flask - How to display a selected dropdown value in same html page?
[英]Flask how to set HTML dropdown selected value based on current choice
我的 Flask 应用程序中有一个页面,用户可以在其中查询表的最后 10、25、50 或 100 个条目。 我希望下拉默认选择显示用户选择的条目数。 因此,如果他们决定要显示 50 个条目,则将在下拉菜单中选择选项50 。
我想我已经接近了,但是我下面的代码没有达到我的目标:
应用程序.py:
class log_db(db.Model):
id = db.Column(db.Integer, primary_key=True)
carrier = db.Column(db.String(100), nullable=False)
@app.route('/history', methods=['GET'])
def history():
if not request.args.get('log'):
query_limit = "10"
else:
query_limit = request.args.get('log')
log = log_db.query.order_by(log_db.id.desc()).limit(query_limit).all()
return render_template('history.html', log=log)
历史.html:
<form class="form">
<label for="log">Number of change log entries to query:</label>
<select name="log" id="log_query">
<option value="10"
{% if query_limit == 10 %} selected {% endif %}>10</option>
<option value="25"
{% if query_limit == 25 %} selected {% endif %}>25</option>
<option value="50"
{% if query_limit == 50 %} selected {% endif %}>50</option>
<option value="100"
{% if query_limit == 100 %} selected {% endif %}>100</option>
</select><br><br>
<input type="submit" value="Update View" class="create_edit">
</form>
在您的 render_template 返回中,您没有通过“query_limit”。 注意比较字符串和整数它无法工作。
我编辑了代码并让它开始工作。 我将query_limit
传递给history.html
并在 select 标签中的每个数字周围加上引号。
应用程序.py
@app.route('/history', methods=['GET'])
def history():
if not request.args.get('log'):
query_limit = "10"
else:
query_limit = request.args.get('log')
log = log_db.query.order_by(log_db.id.desc()).limit(query_limit).all()
return render_template('history.html', log=log, query_limit=query_limit)
历史.html
<form class="form">
<label for="log">Number of change log entries to query:</label>
<select name="log" id="log_query">
<option value="10"
{% if query_limit == "10" %} selected {% endif %}>10</option>
<option value="25"
{% if query_limit == "25" %} selected {% endif %}>25</option>
<option value="50"
{% if query_limit == "50" %} selected {% endif %}>50</option>
<option value="100"
{% if query_limit == "100" %} selected {% endif %}>100</option>
</select><br><br>
<input type="submit" value="Update View" class="create_edit">
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.