MVS*_*MVS 2 html python select jinja2 flask
我正在尝试获取一个选择选项,该选项在使用 Flask 刷新页面后被选中。我曾尝试使用 Jinga2 这样做,但它不起作用:
<div class="col-sm-4 col-lg-4 col-md-4">
<select class="form-control" id="myselect" name="thing" required>
<option value="" {% if thing=='' %} selected {% endif %} ></option>
<option value="Foo" name="Foo" id="Foo" {% if thing =="Foo" %} selected {% endif %}>Foo</option>
<option value="Bar" name="Bar" id="Bar" {% if thing =="Bar" %} selected {% endif %}>Bar</option>
</select>
</div>
Run Code Online (Sandbox Code Playgroud)
energy使用 Python 填充和传递变量的位置。在研究了这个之后,我觉得这是在 Flask 中进行这项工作的方式,尽管显然不是。任何援助将不胜感激!
@app,route('/things', methods=['POST']
def things()
if len(facts['thing']) > 11:
energy = [facts['thing'][0:8],facts['thing'][9:]]
else:
energy = [facts['things']]
...
return render_template('thing.html', thing=energy)
Run Code Online (Sandbox Code Playgroud)
请参阅此示例,因为它适用于您正在尝试执行的操作。我无法完全调试您的代码中出了什么问题,因为您为我提供了零件,而我不知道他们在做什么。
文件夹结构
Test
|___templates
| |___things.html
|___Test.py
Run Code Online (Sandbox Code Playgroud)
东西.html
<form method="post">
<div class="col-sm-4 col-lg-4 col-md-4">
<select title="thing" class="form-control" id="myselect" name="thing" required>
<option value="" {% if thing=='' %} selected {% endif %} ></option>
<option value="Foo" name="Foo" id="Foo" {% if thing =="Foo" %} selected {% endif %} >Foo</option>
<option value="Bar" name="Bar" id="Bar" {% if thing =='Bar' %} selected {% endif %}>Bar</option>
</select>
<button type="submit">SEND</button>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
测试文件
from flask import Flask, render_template, request
app = Flask(__name__)
PORT = 5000
@app.route('/things', methods=['GET', 'POST'])
def things():
"""
Accepts both GET and POST requests. If it's a GET request,
you wouldn't have a last selected thing, so it's set to an
empty string. If it's a POST request, we fetch the selected
thing and return the same template with the pre-selected
thing.
You can improve on this and save the last selected thing
into the session data and attempt to retrieve it from there.
"""
thing = ''
if request.method == 'GET':
return render_template('things.html', thing=thing)
else:
thing = request.form.get('thing', '')
return render_template('things.html', thing=thing)
if __name__ == '__main__':
app.run(port=PORT)
Run Code Online (Sandbox Code Playgroud)