【问题标题】:Retreive value from 2nd tuple in WTForms SelectField从 WTForms SelectField 中的第二个元组中检索值
【发布时间】:2017-11-01 08:46:36
【问题描述】:

首先,还有另一个问题here 询问有关检索元组值的问题,但我已经尝试过这个建议,但它不适合我的需要。(建议使用[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 %}

这是我的 results.html 文件,用于显示已选择的选项。

<!-- extend base layout -->
{% extends "base.html" %}

{% block content %}


<table border=1 >
<tr>
        <th>Disc Number</th>
        <th>Staff Name</th>
<tr>
<td> {{ form.ranumber.data }} </td>
<td> {{ form.staff.data }} </td>
</tr>
</table>




{% endblock %}

下拉菜单正常工作,在上一页输入的信息将打印在 results.html 页面上。但是,当我打电话时:

{{ form.staff.data }}

正在打印 SelectField 中的第一个数据元组。我想要的是调用工作人员的实际姓名,例如,与其打印'2',不如打印'John Thomas'。如何调用第二个数据元组(即在这种情况下为名称)而不是第一个?

谢谢

【问题讨论】:

标签: python flask wtforms flask-wtforms


【解决方案1】:

如果你只想在 results.html 中显示名称,你可以在视图中获取名称,然后将其作为变量 (staff_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_name }} 获取名称。

顺便说一句,您可以将选择的索引更改为与其名称相同,然后您的{{ form.staff.data }} 将可以显示名称。

【讨论】:

  • 您好感谢您的回复。我想你可能误解了我的问题。我的 results.html 旨在显示在 index.html 中选择的选项(为了清楚起见,我现在添加了该选项)。我在 results.html 中有一个表格,从 index.html 的下拉列表中选择的用户将显示在 results.html 的表格中,但是目前它显示的是数字,而不是他们的名字。
  • 好的,我明天回到我的工作电脑上试试这个,我会让你知道它是怎么回事,如果它有效,我会接受答案。
  • 嗯,我收到了File /home/flask_tutorial/app/views.py", line 38, in results title='Results', form=form, staff_name = dict(staff_choices).get(form.staff.data)) NameError: name 'staff_choices' is not defined 我什至已经将 staff_choices 变量行放在了 StaffNames 类中,但它仍然无法正常工作。
  • 不要将staff_choices 放在StaffNames 类中,只需确保staff_choices 可以被您的函数def results(): 使用,查看我的答案以了解变量staff_choices,它可以通过 StaffNames 类和函数访问。
  • 啊问题是我需要将它作为函数从表单导入到 view.py 文件中。现在可以了,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-07
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
相关资源
最近更新 更多