【发布时间】:2022-01-19 02:23:32
【问题描述】:
公司有工人在一天中进行各种活动。每个活动都有start_time 和finish_time。工作人员通常忘记发出活动结束的哔哔声(finish_time),这是因为有一个存储过程read_unended time_from time_to 读取time_from 和time_to 之间的记录,而finish_time 没有finish_time(是NULL )。
例如
id name day start_time finish_time place activity
38 Thomas 2021-12-03 2021-12-03 08:51:38.000 NULL p1 a1
28 Charles 2021-12-02 2021-12-02 12:29:03.000 NULL p2 a2
49 John 2021-12-06 2021-12-06 11:59:48.000 NULL p3 a3
68 Jessie 2021-12-08 2021-12-08 10:55:12.000 NULL p4 a4
82 Susanne 2021-12-10 2021-12-10 12:38:03.000 NULL p5 a5
(forms.py)中有一个表格
class FromToForm(Form):
start_date = DateField(widget=AdminDateWidget())
start_time = TimeField(widget=AdminTimeWidget())
end_date = DateField(widget=AdminDateWidget())
end_time = TimeField(widget=AdminTimeWidget())
(views.py) 中有一个视图显示了这样的表格。
def ending(req):
from_to_form = FromToForm()
result = []
context = {
'form': from_to_form,
'result': result
}
if req.method == "POST":
from_to_form = FromToForm(req.POST)
if from_to_form.is_valid():
start = datetime.combine(from_to_form.cleaned_data['start_date'], from_to_form.cleaned_data['start_time']).isoformat()
end = datetime.combine(from_to_form.cleaned_data['end_date'], from_to_form.cleaned_data['end_time']).isoformat()
with connections["mssql_database"].cursor() as cursor:
cursor.execute("EXEC read_unended @dt_od='%s', @dt_do='%s'" % (start, end))
result = cursor.fetchall()
context['result'] = result
return render(req, 'ending.html', context)
else:
return render(req, 'ending.html', context)
else:
return render(req, 'ending.html', context)
以及templates.py 中的关联模板。
<form action='.' method='POST'>{% csrf_token %}
{{ form.media }}
{{ form.as_p }}
<input type='submit' value='Read unended' class="btn btn-secondary" />
</form>
{% if result %}
<table class="table mb-0">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>day</th>
<th>start_time</th>
<th>finish_time</th>
<th>place</th>
<th>activity</th>
</tr>
</thead>
<tbody>
{%for i in result %}
<tr>
<td>{{i.0}}</td>
<td>{{i.1}}</td>
<td>{{i.2}}</td>
<td>{{i.3}}</td>
<td>CELL TO INSERT END TIME*</td>
<td>{{i.5}}</td>
<td>{{i.6}}</td>
<td>BUTTON TO FINISH THIS ACTIVITY**<td/>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
Every activity is ended
{% endif %}
** 和 * 尚未实现。
我想实现以下功能。在动态生成的表格的每一行中,都应该有一个按钮** 来完成这个活动(这一行),时间是应用程序的用户插入的时间。在那一刻,页面应该刷新并且该行不应该再显示了,因为这个活动已经分配了finish_time。如何实现这样的视图和模板?我需要在现有表单中添加动态生成的字段吗?你有什么建议?
【问题讨论】:
-
安全警报:
cursor.execute("EXEC read_unended @dt_od='%s', @dt_do='%s'" % (start, end))允许 sql 注入攻击。改用这个cursor.execute("EXEC read_unended @dt_od='%s', @dt_do='%s'" ,(start, end))
标签: django ajax django-views django-forms