按下按钮后更改价值形式(django)

时间:2019-07-01 18:03:40

标签: html mysql django python-2.7

我有一个表单,当我选择我的选项并按下“选择”按钮时,我需要使用所选对象的数据来更新表单。我的问题是,当我执行静态对象时,{%for%}已经将我标记为错误,因为它不是列表。我不知道这是否是正确的方法。

这正在运行Mysql,django 1.11和python 2.7.15

views.py

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

def seleccion(request):
    alumno = Alumnos.objects.get(pk=request.POST['Nombre'])
    context = {'alumno': alumno}
    return render(request, 'competencias_app/competencias.html', context)

competencias.html


 <form action="/seleccion" method="POST">
            {% csrf_token %}
            <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">
                    {% for alumno in alumno %}
                    <option value="{{alumno.idAlumnos}}">{{alumno.nombre}}</option>
                    {% endfor %}
                    <input type="submit" name="Seleccionar">

                </select>
            </div>
            <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>

按“ seleccionar”时的输出是

请求方法:POST 要求网址:http://localhost:8000/seleccion Django版本:1.11.21 异常类型:TypeError 异常值:
“校友”对象不可迭代

Images for more details

1 个答案:

答案 0 :(得分:0)

如果我不知道是不是正确的解决方案,但是可以用,我就解决我的问题!

competencias.html

 <form action="/seleccion" method="POST">
            {% csrf_token %}
            <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">

                    {% if alumno|length > 1 %}
                    {% for alumno in alumno %}
                    <option value="{{alumno.idAlumnos}}">{{alumno.nombre}}</option>
                    {% endfor %}
                    {% else %}
                    <option value="{{alumno.idAlumnos}}">{{alumno.nombre}}</option>
                    {%endif%}

                    <input type="submit" name="Seleccionar">

                </select>
            </div>
            <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>

views.py

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

def seleccion(request):
     lstCompetencias = []
     alumno = Alumnos.objects.get(pk=request.POST['Nombre'])
     for p in Competencias.objects.raw('Select * from test_app_competencias where idmapasfk_id = %s', [request.POST['Nombre']]):
         lstCompetencias.append(p)
     context = { 
         'alumno' : alumno,
         'competencias' : lstCompetencias
        }

     return render(request, 'competencias_app/competencias.html', context)