[Django]-Display a grid radio form with Django Forms or Django Floppyforms

4πŸ‘

βœ…

I’d do this with a django form with one field per line on your grid, with a ChoiceField and RadioSelect as widget:

line1 = forms.ChoiceField(widget=forms.RadioSelect, choices=TRUC)
line2 = …

Then you need to find the template layout to render this correctly. RadioSelect generates a <ul> with eahc choice being a <li>. The choice label is included in the <li>, which is not what you want here.

With floppyforms you could subclass RadioSelect and give it a cutom template_name (look at the floppyforms/radio.html template) to omit the labels. You can also alter it to render, say, table cells (<td>s) instead of <li>, and make your form markup generate a table with the labels in the table’s header.

import floppyforms as forms

class RadioSelect(forms.RadioSelect):
    template_name = 'myforms/radio.html'

Put your desired row markup in the above template.

Then, in the template that renders your form:

<form method="post" action="youraction">
  <table>
    <thead>
      <td> <!-- put your form headers here --> </td>
    </thead>
    <tbody>
      {% for field in form %}
        <tr>
          <td>{{ field.label_tag }}</td>
          {{ field }}
        </tr>
      {% endfor %}
    </tbody>
  </table>
<!-- submit button -->
</form>

Then it’s only a matter of applying a couple of CSS styles to get the layout you need πŸ™‚

πŸ‘€brutasse

1πŸ‘

You can try this field django-radiogrid

Leave a comment