Skip to content
Advertisement

How to show more objects in my html with django (queries)

I want to show more queries within my html page but it only shows a single object. I do not know how to add more

This is in mysql, html, django1.11 and python 2.7.15

views.py

   def administrador(request):
    alumno = Alumnos.objects.get(pk=1)
    context = { 'alumno': alumno }
    mapa = mapas.objects.get(pk=4)
    context2 = { 'mapa' : mapa}
    competencias = Competencias.objects.get(pk=1)
    context3 = { 'competencias' : competencias}
    return render(request, 'competencias_app/competencias.html',context)
<form action="/competencia" method="POST">
          {% csrf_token %}

            <div class="container-alumnos">
                <div>
                <select id="carrera" name="Carrera">
                    <option value="1">TICS</option>
                    <option value="2">Carrera</option>
                    <option value="3">Carrera</option>
                    <option value="4">Carrera</option>
                    <option value="5">Carrera</option>
                </select>  
                </div>

                <div>
                    <select id="Alumno" name="Nombre">
                        <option value="{{alumno.idAlumnos}}">{{alumno.nombre}}</option>
                    </select> 
                </div>
                <form action="">
                    <label for="ID">ID</label>
                    <input type="input" name="id" disabled value="{{alumno.idAlumnos}}"><br>
                    <label for="apellidos">Apellidos</label>
                    <input type="input" name="apellidos" disabled value="{{alumno.apellidos}}"><br>
                    <label for="Correo">Correo</label>
                    <input type="input" name="Correo" disabled value="{{alumno.correo}}"><br>
                </form>

            </div>

            <select id="opciones" name="Semana">
                    <option value="1">Semana 1</option>
                    <option value="2">Semana 2</option>
                    <option value="3">Semana 3</option>
                    <option value="4">Semana 4</option>
                    <option value="5">Semana 5</option>
                </select>
            <div class="container-listas">

                <select name="OS" size=9>
                    <option selected value="0"> Elige una opciĆ³n </option>
                        <optgroup label="{{mapa.nombre}}"> 
                        <option value="1">{{competencias.nombre}}</option> 
                        <option value="2">Nombre Competencia</option> 
                        <option value="3">Nombre Competencia</option> 
                    </optgroup> 
                    <optgroup label="Mapa - Nombre"> 
                        <option value="10">Nombre Competencia</option> 
                        <option value="11">Nombre Competencia</option> 
                        <option value="12">Nombre Competencia</option> 
                    </optgroup> 
                </select>

i need add object mapa and competencias. The object alumno i dont have problem

Advertisement

Answer

User filter instead of get. Because, get returns a single object, but filter returns all matching objects.

def administrador(request):
    alumno = Alumnos.objects.filter(pk=1)
    mapa = mapas.objects.filter(pk=4)
    competencias = Competencias.objects.filter(pk=1)
    context = {
        'alumno': alumno,
        'mapa': mapa,
        'competencias': competencias
    }
    return render(request, 'competencias_app/competencias.html', context)

In your template

For Alumnao,

<select id="alumno" name="alumno">
{% for item in alumno %}
     <option value="{{item.id}}">{{item.your_desired_field}}</option>
{% endfor %}
</select> 

For mapa,

<select id="mapa" name="mapa">
{% for item in mapa %}
     <option value="{{item.id}}">{{item.your_desired_field}}</option>
{% endfor %}
</select> 

For competencias,

<select id="competencias" name="competencias">
{% for item in competencias %}
     <option value="{{item.id}}">{{item.your_desired_field}}</option>
{% endfor %}
</select> 
Advertisement