Python 从WTForms SelectField中的第二个元组检索值
首先,还有一个问题是关于检索元组值的,但我尝试了这个建议,但它不符合我的需要。(建议使用Python 从WTForms SelectField中的第二个元组检索值,python,flask,wtforms,flask-wtforms,Python,Flask,Wtforms,Flask Wtforms,首先,还有一个问题是关于检索元组值的,但我尝试了这个建议,但它不符合我的需要。(建议使用[1]调用第二个值,即{{form.staff.data[1]}——这对我来说不起作用 我创建了这个表单,它基本上是一个下拉菜单,其中包含员工姓名: class StaffNames(Form): staff = SelectField( 'staff', choices=[("", ""), ('1', 'John Thomas'), ('2', 'Chris
[1]
调用第二个值,即{{form.staff.data[1]}
——这对我来说不起作用
我创建了这个表单,它基本上是一个下拉菜单,其中包含员工姓名:
class StaffNames(Form):
staff = SelectField(
'staff',
choices=[("", ""), ('1', 'John Thomas'), ('2', 'Chris Davis'), ('3', 'Lyn Taco')], validators=[DataRequired()]
)
以下是“我的视图”文件中的相关信息,下拉列表位于“索引”页上,结果显示在“结果”页上:
@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
def index():
form = StaffNames()
if form.validate_on_submit():
return redirect('/results')
return render_template('index.html',title='Search Page',form=form)
@app.route('/results', methods=['GET', 'POST'])
def results():
form =StaffNames()
return render_template('results.html',
title='Results',form=form)
以下是显示下拉列表的index.html:
<!-- extend base layout -->
{% extends "base.html" %}
{% block content %}
<center>
<h1>Search</h1>
<form action="results" method="post" name="indexsearch">
{{ form.hidden_tag() }}
<p>{{ form.ranumber }} Enter RA Number</p>
{% for error in form.ranumber.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}<br>
<p>{{ form.staff }} Select your name</p>
{% for error in form.staff.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}<br>
<p><input type="submit" value="Search"></p>
</form>
{% endblock %}
正在打印SelectField中的第一个数据元组。我想调用的是员工的实际姓名,因此,例如,不打印“2”,它应该打印“John Thomas”。如何调用第二个数据元组(即,在本例中是名称),而不是第一个
谢谢如果您只想在results.html中显示名称,您可以在视图中获取名称,然后将其作为变量(
staff\u name
)传递给模板,如下所示:
staff_choices=[("", ""), ('1', 'John Thomas'), ('2', 'Chris Davis'), ('3', 'Lyn Taco')]
class StaffNames(Form):
staff = SelectField('staff',choices=staff_choices)
@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
def index():
form = StaffNames()
if form.validate_on_submit():
return redirect('/results')
return render_template('index.html',title='Search Page',form=form)
@app.route('/results', methods=['GET', 'POST'])
def results():
form =StaffNames()
return render_template('results.html',
title='Results',form=form, staff_name = dict(staff_choices).get(form.staff.data))
在results.html模板中,使用{{staff\u name}}
获取名称
顺便说一句,你可以将你选择的索引更改为它们的名称,然后你的
{{form.staff.data}
将显示名称。可能重复@WillDaSilva我看到了这个答案,似乎建议我应该调用{{form.staff.data[1]}但这不会产生结果。您好,谢谢您的回答。我想您可能误解了我的问题。my results.html旨在显示index.html中选择的选项(为了清晰起见,我现在添加了该选项)。我在results.html中有一个表,用户从index.html下拉列表中选择的内容将显示在results.html中的表中,但是现在它显示的是数字,而不是他们的名字。好的,我明天回到我的工作电脑上时会试试这个,我会让你知道它是如何运行的,并接受答案。嗯,我得到了File/home/flask_tutorial/app/views.py”,第38行,在results title='results',form=form,staff_name=dict(staff_choices).get(form.staff.data))NameError:name'staff_choices'未定义
我甚至将staff_choices变量行放在StaffNames类中,它仍然不起作用。不要将staff_choices
放在StaffNames类中,只要确保staff_choices
可以被函数def results()使用即可:
,检查我的答案以了解变量staff\u选项
,该变量可由类StaffName和函数访问。啊,问题是我需要将其作为函数从表单导入view.py文件。现在它可以工作了,谢谢!
{{ form.staff.data }}
staff_choices=[("", ""), ('1', 'John Thomas'), ('2', 'Chris Davis'), ('3', 'Lyn Taco')]
class StaffNames(Form):
staff = SelectField('staff',choices=staff_choices)
@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
def index():
form = StaffNames()
if form.validate_on_submit():
return redirect('/results')
return render_template('index.html',title='Search Page',form=form)
@app.route('/results', methods=['GET', 'POST'])
def results():
form =StaffNames()
return render_template('results.html',
title='Results',form=form, staff_name = dict(staff_choices).get(form.staff.data))