如何在django views.py中动态添加复选框值?

时间:2017-10-21 15:06:23

标签: javascript django python-3.x django-views

我希望我的views.py中的check复选框值和复选框是使用javascript动态添加的 这是我的HTML代码

<html>
<head>
    <title>Department Attendance</title>
    <script language="javascript" type="text/javascript">
            function dynamicdropdown(listindex) {
                var tbl = document.getElementById('tbl');
                while (tbl.rows.length > 2) {
                    tbl.deleteRow(2);
                }
                switch (listindex) {
                    case "1" :
                        var r = 2;
                    {% for s in student %}
                        {% if s.division.did == 1 %}
                            var x = tbl.insertRow(r);
                            var y0 = x.insertCell(0);
                            var y1 = x.insertCell(1);
                            var y2 = x.insertCell(2);
                            var y3 = x.insertCell(3);
                            tbl.rows[r].cells[1].innerHTML = {{s.roll}};
                            tbl.rows[r].cells[2].innerHTML = "{{s.sname}}";
                            tbl.rows[r].cells[3].innerHTML = "{{s.addr}}";
                            var checkbox = document.createElement('input');
                            checkbox.type = "checkbox";
                            checkbox.name = "checkbox";
                            checkbox.value = {{ s.roll }};
                            checkbox.id = "checkbox";
                            y0.appendChild(checkbox);
                            r++;
                        {% endif %}
                    {% endfor %}
                        break;
                    case "2":
                        var r = 2;
                    {% for s in student %}
                        {% if s.division.did == 2 %}
                            var x = tbl.insertRow(r);
                            var y0 = x.insertCell(0);
                            var y1 = x.insertCell(1);
                            var y2 = x.insertCell(2);
                            var y3 = x.insertCell(3);
                            tbl.rows[r].cells[1].innerHTML = {{s.roll}};
                            tbl.rows[r].cells[2].innerHTML = "{{s.sname}}";
                            tbl.rows[r].cells[3].innerHTML = "{{s.addr}}";
                            var checkbox = document.createElement('input');
                            checkbox.type = "checkbox";
                            checkbox.name = "checkbox";
                            checkbox.value = {{ s.roll }};
                            checkbox.id = "checkbox";
                            y0.appendChild(checkbox);
                            r++;
                        {% endif %}
                    {% endfor %}
                        break;
                    default:
                        break;
                }
            }
    </script>
</head>
<body>
<form action="{% url 'App1:submit' %}" method="POST">{% csrf_token %}
    <select name="dropdown" id="dropdown" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);">
      <option value="">Please select Division</option>
      <option value="1">Div-A</option>
      <option value="2">Div-B</option>
    </select>
    <table name="tbl" id="tbl" border="double" width="865">
        <th colspan="6">Select Student</th>
        <tr>
            <th>Select</th>
            <th>Roll No</th>
            <th>Name</th>
            <th>Address</th>
        </tr>
         </table>
    <input type="submit" value="Submit" name="submit"  />
</form>
</body>
</html>

以下是我的views.py

from django.shortcuts import render
from .models import student

def index(request):
    obj1=student.objects.all()
    return render(request,'index.html',context={'student':obj1})

def submit(request):
    check = request.POST.getlist('checkbox')
    print(check)
    return render(request,'attendance.html')

有两个区分,在从下拉菜单选择特定区分的时候那个区分学生名单如下图所示 enter image description here

但是当我们选中复选框并提交时,check会在views.py中不包含任何值 我的代码有问题吗?或任何其他方式来做到这一点?

0 个答案:

没有答案