Search code examples
pythondjangojinja2

django bootstrap select element one column


i use djando and bootstrap. I use a form with a method POST and "select" option. I try to select elements belong to a table. I can see the database elements, but elements it's not showing in a column, they showing in a row (one consecutive to another).

I try to figure what is the error, but i not fix it. It's my firts time with django and python;-)!

Thanks in advance!

Elements in a row

The code:

`
<form method="POST" action="{% url 'consorcio' %}">{% csrf_token %}
                        <div class="row">
                            <div class="col">
                                <input type="text" class="form-control" id="name_consorcio" required name="name_consorcio" placeholder="Nombre Consorcio:"><br/>
                            </div>   
                            <div class="col">
                                <input type="text" class="form-control" id="direccion_consorcio" required name="direccion_consorcio" placeholder="Direccion Consorcio:"><br/>
                            </div>
                            <div class="col">
                                <input type="text" class="form-control" id="cuit" required name="cuit" placeholder="CUIT:"><br/>
                            </div>
                            <div class="col">
                                <select class="form-select">
                                    <option selected>Seleccionar Administracion:
                                        <option value="1">
                                                <ul>
                                                {% for a in all_consorcio %} 
                                                    <li>{{ a.administracion.name_administracion }}</li>
                                                {% endfor %}
                                                </ul>
                                        </option>
                                    </option>
                                </select>
                            </div>   
                        </div>
                        <input type="submit" class="btn btn-primary" value="Guardar">`
                </form><br>

I tried to modificate the blucle for and add other stuff, like
and , etc...


Solution

  • I think this is the format you want:

    Each all_consorcio item will be it's own an option in the Dropdown

    • 5 items in the list => 5 (+1 default) options in the dropdown.
    <div class="col">
      <select class="form-select" name="SOMEFIELD_consorcio">
        <option selected>Seleccionar Administracion:</option>
    
        {% for a in all_consorcio %} 
          <option value="{{ a.pk }}">
            {{ a.administracion.name_administracion }}
          </option>
        {% endfor %}
    
      </select>
    </div>   
    

    Note: change SOMEFIELD_consorcio to the actual field name

    Per value

    the value attribute is what will be posted when the form submits. I've set it as {{ a.pk }} as I'm assuming the form field is a ForignKey; if it's not try changing it to {{ a.administracion.name_administracion }} or some other value.