oTree Forum >

Getting each formfield choice individually

#1 by jkemp

Hi all,
we have a post-game survey in which we will have a survey item with four substantive and two non-substantive answer options. These are choices 99 and 88 in the code below. The non-substantive answer option must be visually separated from the other options.

My idea is to construct a table in a similar way to the example app complex_form_layout, but to construct the table manually and not iterate over all the choices.
I'd put the choices 1-4 in the top part of the table, draw a <hr> line or similar and then put the choices 88 and 99 in the bottom part of the table. 
To do this, I tried to treat {{ form.natio1.choices }} as a list (e.g. tried {{ form.natio1.choices[1] }}) or dict (e.g. tried {{ form.natio1.choices["1"] }}), but was unable to access the fields individually.

How Do I get each formfield choice individually?

Thank you in advance,
Jakob

Relevant __init__.py code:

class Player(BasePlayer):
    natio1 = models.IntegerField(
        label="Wie stolz sind Sie, Deutsche(r) zu sein?",
        choices=[[1, 'Sehr stolz'], [2, 'Ziemlich stolz'], [3, 'Nicht sehr stolz'], [4, 'Überhaupt nicht stolz'],
                 [99, 'Bin kein(e) Deutsche(r)'], [88, 'Kann ich nicht sagen']],
        widget=widgets.RadioSelect)

class PageNatio1(Page):
    form_model = 'player'
    form_fields = ['natio1']

#2 by Chris_oTree

Maybe separate it into 2 fields ['natio1', 'natio1_non_substantive'], each with a separate choices= list, then use the error_message function to ensure that the user didn't select both. (and set blank=True).

Or keep it as 1 field but write the raw HTML of the widget, rather than using oTree's form rendering.

#3 by jkemp (edited )

Hi Chris,
thank you for the reply. I solved it with the raw HTML.
The HTML on the page is now:

{{ extends 'global/Page.html' }} {{block title}}
Fragebogen zur Studie
{{endblock}}


{{ block content }}


<div class="mb-3 _formfield">

    <label class="col-form-label" for="id_natio1">Wie stolz sind Sie, Deutsche(r) zu sein?</label>
    <div class="controls">
        <div id="id_natio1">
            <div class="form-check"><input class="form-check-input" type="radio" id="id_natio1-0" name="natio1"
                                           value="1"> <label for="id_natio1-0">Sehr stolz</label></div>
            <div class="form-check"><input class="form-check-input" type="radio" id="id_natio1-1" name="natio1"
                                           value="2"> <label for="id_natio1-1">Ziemlich stolz</label></div>
            <div class="form-check"><input class="form-check-input" type="radio" id="id_natio1-2" name="natio1"
                                           value="3"> <label for="id_natio1-2">Nicht sehr stolz</label></div>
            <div class="form-check"><input class="form-check-input" type="radio" id="id_natio1-3" name="natio1"
                                           value="4"> <label for="id_natio1-3">Überhaupt nicht stolz</label></div>
            <hr>
            <div class="form-check"><input class="form-check-input" type="radio" id="id_natio1-4" name="natio1"
                                           value="99"> <label for="id_natio1-4">Bin kein(e) Deutsche(r)</label></div>
            <div class="form-check"><input class="form-check-input" type="radio" id="id_natio1-5" name="natio1"
                                           value="88"> <label for="id_natio1-5">Kann ich nicht sagen</label></div>
        </div>
    </div>


</div>

{{ formfield_errors 'natio1' }}

{{ next_button }}

{{ endblock }}

Write a reply

Set forum username