【发布时间】:2023-03-14 01:23:01
【问题描述】:
我可以知道为什么当为特定行选择“批准”或“拒绝”按钮时我的状态保持不变?如何将我在 views.py 中更改的值传递回我的 html 文件以打印出状态?我应该在 models.py 中声明 Action 字段,因为 action 只是两个按钮,名称分别为批准和拒绝?
.html 文件
<form method="POST" action="">
{% csrf_token %}
<table id="example" class="display" cellspacing="0" width="100%" border="1.5px">
<tr align="center">
<th> Student ID </th>
<th> Student Name </th>
<th> Start Date </th>
<th> End Date </th>
<th> Action </th>
<th> Status </th>
</tr>
{% for item in query_results %}
<tr align="center">
<td> {{item.studentID}} </td>
<td> {{item.studentName}} </td>
<td> {{item.startDate|date:'d-m-Y'}} </td>
<td> {{item.endDate|date:'d-m-Y'}} </td>
<td> <input type="submit" name="approve" id="approve" value="approve" > <input type="submit" name="deny" id="deny" value="deny" > </td>
<td>
{% if not item.action %}
Pending
{% endif %}
</td>
</tr>
{% endfor %}
</table>
</form>
当我尝试在控制台中打印 query_results 时,我看不到状态已更改。
views.py
def superltimesheet(request):
query_results = Timesheet.objects.all()
data={'query_results':query_results}
supervisor = SuperLTimesheet.objects.all()[0]
if request.method == 'POST':
if request.POST.get('approve'):
supervisor.status = "approved"
supervisor.save()
return redirect('/hrfinance/superltimesheet/')
elif request.POST.get('deny'):
supervisor.status = "denied"
supervisor.save()
return redirect('/hrfinance/superltimesheet/')
return render(request, 'hrfinance/supervisor_list_timesheet.html', data)
模型.py
class SuperLTimesheet(models.Model):
timesheet = models.ForeignKey(Timesheet, on_delete=models.CASCADE, null=True)
status = models.TextField("Status", default="pending", null=True)
【问题讨论】:
标签: python html django button html-table