[Fixed]-Django, how can I loop through the choices in HTML and display in radio buttons?

1👍

You didn’t paste the AddChoiceForm code but I can imagine how it looks like.

The thing is that you are iterating through the form (it’s form fields) and generating the radio inputs on your own and in the next line you are rendering the default html of the question form field which in this case is a select.

You can do two thing from here:

  1. Drop the {{ question }} and render the field completely by your own – you will have access to the fields attributes (like choices) in the template.

  2. Change the form widget in your form class. More info here: https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/ and then drop the idea of rendering the radio on your own.

If I may – I’d suggest you to follow a style guide – I see you are mixing up a little and you can find your code messy in a while. For e.g questionText should be question_text. And when you render the whole dict you are passing is the context so naming the form by context is not a great idea (for example took me a while to decipher what you are doing there).

👤McAbra

Leave a comment