(This is a ‘Note to Self’ post)
I’ve been trying for half the day to get radio buttons in a custom template to work. The buttons are generated in a for loop that builds a table row for each choice (iterating over a queryset called cls
), including this code for the row’s radio button:
input type="radio" name="reg_choice_1" value="{{ cls.id }}" {% ifequal srform.reg_choice_1 cls.id %}CHECKED{% endifequal %}
The goal is to have the ifequal
test tell me if the row’s cls.id
matches the value set in the form. However, I didn’t know how to access that value. After many Google searches, perusal of the Django documentation, and even a failed attempt to follow the template rendering code, I was stumped. As a wild guess, I tried the .data
attribute, and it worked! Here’s the updated (and functional) code:
input type="radio" name="reg_choice_1" value="{{ cls.id }}" {% ifequal srform.reg_choice_1.data cls.id %}CHECKED{% endifequal %}
I hope that I’ve put enough keywords in this post so that future stumped Djangonauts can find some help.
As always, leave your comments below.