Firstly, there is another question here that asks about retrieving tuple values, but I have tried the suggestion and it doesn't work for my needs.(the suggestion being to use a [1]
to call the 2nd value i.e. {{ form.staff.data[1] }}
- this hasn't worked for me.
I've created this form which is basically a drop down menu with members of staff names in it:
class StaffNames(Form):
staff = SelectField(
'staff',
choices=[("", ""), ('1', 'John Thomas'), ('2', 'Chris Davis'), ('3', 'Lyn Taco')], validators=[DataRequired()]
)
Here's the relevant info from my views file, the drop-down is on the index page and the result ends up on the results page:
@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)
Here's the index.html that displays the drop down:
<!-- 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 %}
Here is my results.html file which is intended to display the option that was selected.
<!-- 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 %}
The drop-down is working as it should and information entered on the previous page prints on the results.html page. However, when I call:
{{ form.staff.data }}
the first tuple of data from the SelectField is being printed. What I would like is to call the actual name of the member of staff, so for example rather than print '2' it should print 'John Thomas'. How do I call the 2nd tuple of data (i.e. in this case the name) rather than the first?
Thanks