django - 如何在 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>
解决方案
推荐阅读
- kdb - 列出不执行自动展开的附加运算符
- javascript - javascript函数不采取形式价值?
- java - 每次运行应用程序时都会遇到这个问题: java.lang.NoClassDefFoundError: Class not found using the boot class loader; 没有可用的堆栈
- regex - 查询所有符合特定条件的
- html - 上边距拉动顶部元素
- python - 尽管在函数中生成,为什么我的绘图标签会发生变化?
- haskell - 在 Haskell 中解析文本时间戳的文本友好方式
- android - 如何将生成的 Dagger 模块添加到组件
- python - 调用函数时如何添加参数?
- reactjs - React-router typed-url 并在 tomcat、弹性 beanstalk 上刷新