1

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

nmh
  • 491
  • 2
  • 8
  • 23
  • Possible duplicate of [Getting one value from a python tuple](https://stackoverflow.com/questions/3136059/getting-one-value-from-a-python-tuple) – Will Da Silva May 31 '17 at 13:51
  • @WillDaSilva I've seen that answer and it seems to suggest that I should call {{ form.staff.data[1] }} but this doesn't produce a result. – nmh May 31 '17 at 13:57

1 Answers1

1

If you just want to get the name display in the results.html, you can get the name in your view, then pass it to the template as a variable (staff_name) like this:

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))

In your results.html template, use {{ staff_name }} to get the name.

BTW, you can change the index of your choices same as their name, then your {{ form.staff.data }} will do work to show the name.

Tiny.D
  • 6,466
  • 2
  • 15
  • 20
  • HI Thanks for your response. I think you may have misunderstood my question. My results.html is intended to show the choice that was selected in the index.html (which I have added now for clarity). I have a table in results.html and the users choice from the drop down in index.html will be displayed in the table in results.html, however at the moment it's displaying the number, not their name. – nmh May 31 '17 at 15:03
  • ok, i'll try this tomorrow when i'm back on my work PC and i'll let you know how it goes and accept the answer if it works. – nmh May 31 '17 at 21:56
  • hmm, i'm getting `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` I've even put the staff_choices variable line within the StaffNames class and it's still not working. – nmh Jun 02 '17 at 13:16
  • don't put the `staff_choices` in the class StaffNames, just make sure the `staff_choices` can be used by your function `def results():`, check my answer to understand the variable `staff_choices`, which can be accessed by class StaffNames and the functions. – Tiny.D Jun 02 '17 at 13:19
  • ahh the issue was I needed to import it as a function from forms into the view.py file. Now it works, thanks! – nmh Jun 02 '17 at 13:23