【问题标题】:Delete specific Row in Django删除 Django 中的特定行
【发布时间】: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">&times;</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


【解决方案1】:

您正在为 for 循环的每次迭代生成模态对话框的标记,但是您不会更改 id 值。 id 始终设置为 myModal,此时它应该是唯一的。

因此,当您单击按钮时,您可能总是会看到与遇到的第一个 id 对应的对话框。

您可以使用 Django 的 forloop.counter 变量为每个模态分配一个唯一的 id

<div class="modal fade" id="myModal-{{ forloop.counter }}"

从按钮引用时也这样做:

<button class="btn btn-primary" data-toggle="modal" data-target="#myModal-{{ forloop.counter }}"

您还必须确保您的表单提交处理程序onclick="form_submit()" 根据循环迭代提交正确的表单。

【讨论】:

    【解决方案2】:

    如果你想删除特定的行,首先,你必须拿起object id。 例如,使用checkboxes 或其他任何可以获取id(s)

    然后,你需要在你的类/函数中调用这个id,你可以这样写:

    Akte.objects.filter(id=id).delete()
    

    【讨论】:

    • 我认为在我的 for 循环中,当我提交 到我的看法
    猜你喜欢
    • 2021-03-15
    • 1970-01-01
    • 2019-05-11
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多