【发布时间】:2019-02-27 19:00:12
【问题描述】:
我是 Django 和 HTML 的新手,我想创建一个客户门户网站,客户可以在其中管理他们的文档。我的问题是每一行都有自己的删除按钮,但无论我单击哪个按钮,它总是删除顶行而不是我单击的行上的按钮。我认为您只需要查看模板即可查看错误,因为它删除了“Akte”,但不是正确的。
模板:
<table class="center" id="myTable" style="border:solid;border-color:white;padding-bottom:1%;padding-left:1%;margin-top:3%;border-radius:6px;width:50%">
<tr style="margin-left:30%;margin-right:30%;border:solid;border-color:white;padding-bottom:1%;padding-left:1%;margin-top:3%;border-radius:6px;color:white">
<th class="cell" onclick="sortTable(0)">Aktenbarcode</th>
<th class="cell" onclick="sortTable(1)">Ersteller</th>
<th class="cell" onclick="sortTable(2)">Startdatum</th>
<th class="cell" onclick="sortTable(3)">Kundennummer</th>
<th class="cell" onclick="sortTable(4)">Aktionen</th>
</tr>
{%for Akte in akte_list%}
<tr>
<td id="{{Akte.Aktenbarcode}}"class="cell">{{Akte.Aktenbarcode}}</td>
<td class="cell">{{Akte.user}}</td>
<td class="cell">{{Akte.Startdatum}}</td>
<td class="cell">{{Akte.kundennr}}</td>
<td class="cell">
<form action="{% url 'aktedelete' %}" method="post">
{% csrf_token %}
<input class="btn btn-primary" type="submit" value="Status anzeigen"/>
<input type="hidden" name="aktenbarcode" value="{{Akte.Aktenbarcode}}" />
<input type="hidden" name="mitglied" value="{{container}}"/>
<input type="hidden" name="benutzer" value="{{request.user.username}}"/>
<input type="hidden" name="status" value="{{status}}" />
{% if status == "O" %}
<button class="btn btn-primary" data-toggle="modal" data-target="#myModal" type="button"> Akte entfernen</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle" style="color:black">Akte: {{Akte.Aktenbarcode}}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" style="color:black">
Wollen Sie diese Akte unwiderruflich aus dem Container löschen?
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" value="Save" onclick="form_submit()">Ja, ich bin mir sicher</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal" value="Cancel">Nein</button>
</div>
</div>
</div>
</div>
<!-- ModalEnd-->
</form></td>
{% endif %}
</tr>
{% endfor %}
</table>
在我的模态课上:
Akte: {{Akte.Aktenbarcode}}
这个变量也总是显示第一行的“Akte”,而不是我点击按钮的那一行。我希望你能给我一个解释,也许可以解决为什么会发生这种情况。
这是在views.py中:
def aktedelete(request):
if request.method == 'POST':
form = AkteDelForm()
z = AkteForm
container = request.POST['mitglied']
closecontainerform = CloseContainerForm
status = request.POST['status']
aktenbarcode = request.POST.get('aktenbarcode')
akte = Akte.objects.get(Aktenbarcode=aktenbarcode)
akte.delete()
akte_list = Akte.objects.filter(containerId__Containernr=container)
Anzahl_Akten =Akte.objects.filter(containerId__Containernr=container).count
return render(
request,
'app/aktentabelle.html',
{
'form':form,
'title':'About',
'akte_list':akte_list,
'anzahl':Anzahl_Akten,
'container':container,
'aktenform':z,
'status':status,
'closecontainerform': closecontainerform,
'date':datetime.now().date,
}
)
【问题讨论】:
-
如果显示视图会更好
标签: django html-table row