#1
by
Aurel
Hey Chris,
sorry, it is me (again) - thanks for your continuous support! I built a Public Good Game with Punishment according to the example code "punishment" (https://s3.amazonaws.com/otreehub/browsable_source/d7188187-cdba-4c61-ae3a-d7cc3d6fcde9/punishment/index.html). The critical source code is pasted below. In the sample code, you call the punishment fields all together with {{ formfields }}. However, this makes formatting a bit difficult and I would like to call them separately to put them in different rows of a table. I tried a lot of different versions of {{ formfield '...' }}, but I only got error messages as I do not know how to call the fields. Can you help me here please?
Thank you again,
Aurel
%%% SOURCE CODE %%%
def make_punishment_field(id_in_group):
return models.IntegerField(
min=0, max=C.MAX_PUNISHMENT, label="Punishment to player {}".format(id_in_group)
)
class Player(BasePlayer):
contribution = models.CurrencyField(
min=0, max=C.ENDOWMENT, label="How much will you contribute?"
)
punish_p1 = make_punishment_field(1)
punish_p2 = make_punishment_field(2)
punish_p3 = make_punishment_field(3)
punish_p4 = make_punishment_field(4)
cost_of_punishing = models.CurrencyField()
punishment_received = models.CurrencyField()
def get_self_field(player: Player):
return 'punish_p{}'.format(player.id_in_group)
def punishment_fields(player: Player):
return ['punish_p{}'.format(p.id_in_group) for p in player.get_others_in_group()]
#2 by Jonas
Hi Aurel, see: https://otree.readthedocs.io/en/latest/forms.html (the section about "Customizing a field’s appearance"). Does this help?
#3
by
Aurel
Thank you Jonas. Unfortunately, it does not help me as I do not understand what the name of the punishment fields is which I have to use in the HTML. They are created with a function and are dynamic based on the ID of the participant. For example, Participant #1 only sees punish_p2, punish_p3, punish_p4.
#4 by Jonas
Maybe give separate lists to the template and then loop over them? See here: https://s3.amazonaws.com/otreehub/browsable_source/1e38462b-46c7-4ca1-bca5-536462f90131/complex_form_layout/index.html
#5
by
Aurel
Hey Jonas, thank you! Due to your hint, I found a much simpler way:
{% for field in form %}
{% formfield field %}
{% endfor %}
This already gets the job done... Thanks and have a good day!