Placement in MultipleChoiceField template in Django

0

I'm doing a web page in Django and I want to introduce a MultipleChoiceField in a table. I want to introduce it in a personalized way, but when using the template code it does not give results. Here I leave the codes and the result.

  • forms.py

    class paisForm(forms.Form):
        bus_pais = forms.MultipleChoiceField(
            choices=paises.objects.all().order_by('nombre').values_list('reduccion','nombre'),
            widget=forms.CheckboxSelectMultiple,
            required=False,
        )
    
  • views.py

    def index_spider_view(request):
        idiom = idiomas.objects.all().order_by('nombre')
        pais = paises.objects.all().order_by('nombre')
        continentes = paises.objects.values_list('continente', flat=True).distinct().order_by('continente')
    
    
        if request.method == "POST":
            ...
        else:
            buscador_Form = buscador_googleForm()
            idioma_Form = idiomaForm()
            pais_Form = paisForm()
    
        contexto = {'idioma': idiom, 'paises': pais, 'continentes': continentes, 'buscador_Form':buscador_Form,
                    'idioma_Form':idioma_Form, 'pais_Form':pais_Form}
    
        return render(request, 'spider_google/spider.html', contexto)
    
  • Template HTML code:

       <table class="table table-striped text-center tablapais" id="tablpais">
          <thead>
            <tr>
              <th>Seleccion</th>
              <th>Nombre</th>
              <th>Continente</th>
            </tr>
          </thead>
          <tbody id="id_bus_pais">
          {% for pais in pais_Form %}
            <tr>
              <td><input type="checkbox" class="form-control" id="id_bus_pais_{{pais.id_for_label}}" name="idioma_Form" value="{{pais.value}}"></td>
              <td>{{pais.id}}</td>
              <td></td>
            </tr>
          {% endfor %}
          </tbody>
       </table>
    

The result obtained is as follows:

  • HTML Code:

    <tbody id="id_bus_pais">
      <tr>
        <td><input type="checkbox" class="form-control" id="id_bus_pais_" name="idioma_Form" value="None"></td>
        <td></td>
        <td></td>
      </tr>                        
    </tbody>
    

I've tried using {{pais_Form.as_table}} and it works correctly, but it's not what I'm looking for now. I would like if there is an error in the code, because I can not find it and I do not know which is the error for which it does not show the table correctly.

Versions of the programs that intervene in the code:

  • Windows 10 Pro
  • Python 3.6
  • Django 2.0

Thanks to the answers in @F_Delgado's responses and the Django documentation in this section I have managed to obtain the expected result, which was the following:

The code that I have followed is the following:

<tbody id="id_bus_pais">
 {% for pais in pais_Form.bus_pais %}
  <tr>
    <td>{{pais.tag}}</td>
    <td>{{pais.choice_label}}</td>
    <td></td>
  </tr>
 {% endfor %}
</tbody>
    
asked by Alvaro Garcia 08.05.2018 в 08:59
source

1 answer

1

You have an easier way to do it, I explain.

The variable paisForm contains the whole form, within that you will have a field called country that you will call in this way {{paisForm.pais}} which is the one that contains the multiplecheckbox, if you loop it for to said variable you will get all the 1 for 1 options that you can lay out individually. I give you an example:

<tr>
    {% for i in paisform.pais %}
        <td>{{i}}</td>
    {% endfor %}
</tr>
    
answered by 08.05.2018 / 09:38
source