首页 > 解决方案 > 如何在 django 中循环查询?

问题描述

在我的 views.py 文件中,我保存了最后一位不同的表单日期,因此如何循环它们:

if request.method == "POST" :
        staff_id = request.POST.get('staff_id1', None)
        attendances = request.POST.get('attendance1', None)

        date = datetime.date.today()

        ins = attendance(staff_id=staff_id, attendance=attendances, date=date)
        ins.save()

        staff_id = request.POST.get('staff_id2', None)
        attendances = request.POST.get('attendance2', None)

        date = datetime.date.today()

        ins = attendance(staff_id=staff_id, attendance=attendances, date=date)
        ins.save()

        staff_id = request.POST.get('staff_id3', None)
        attendances = request.POST.get('attendance3', None)

        date = datetime.date.today()

        ins = attendance(staff_id=staff_id, attendance=attendances, date=date)
        ins.save()

可以看到staff_id和出勤的最后一位不同,rest是相似的。我想按照表格中的日期循环它。

这是我的表格

<form action="/index" method="POST">
                                            {% csrf_token %}
                                            {% for staffs in staff %}
                                                <input type="hidden" name="staff_id{{staffs.id}}" value="{{staffs.id}}">
                                                <input type="hidden" name="attendance{{staffs.id}}" id='input_attendance{{staffs.id}}'>
                                                <tr>
                                                    <td> {{staffs.id}} </td>
                                                    <td> {{staffs.name}} </td>
                                                    <td>
                                                        <div class="btn-group">
                                                            <button type="button" class="btn btn-outline-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id='present{{staffs.id}}'>
                                                                Present
                                                            </button>
                                                            <div class="dropdown-menu">
                                                                <a class="dropdown-item" onclick="attendance_present({{staffs.id}})">Present</a>
                                                                <a class="dropdown-item" onclick="attendance_absent({{staffs.id}})">Absent</a>
                                                                <a class="dropdown-item" onclick="attendance_half_day({{staffs.id}})">Half-day</a>
                                                            </div>
                                                        </div>
                                                    </td>
                                                    <td> {{staffs.role}} </td>
                                                </tr>
                                                
                                            {% endfor %}
                                            <tr>
                                                <button type="submit" style="float:right" class="btn btn-info">Save</button>
                                            </tr>
                                        </form>

标签: djangodjango-viewsdjango-templates

解决方案


推荐阅读