使用 Django 删除项目按钮仅删除第一项

Posted

技术标签:

【中文标题】使用 Django 删除项目按钮仅删除第一项【英文标题】:Deleting an item button using Django deletes only the first item 【发布时间】:2018-03-20 18:00:25 【问题描述】:

所以我一直在 DJANGO 中建立一个网站,其中包含一个服务器表(IP、所有者等)

我正在使用 mysql

我为每个项目添加了一个删除和编辑按钮。 但是,删除按钮总是删除第一项而不是服务器旁边的按钮!

有行-

我尝试输入 server.id, server.id .. 没有任何效果。 有人知道吗?

附言 当我在确认中询问您是否要删除 server.ServerName.. 它只询问第一台服务器的名称,即使它在循环中..

index.html-

    % for server in posts %

    <tr>
      <div class ="server">
        <td> server.ServerName </td>
        <td> server.Owner </td>
        <td> server.Project </td>
        <td> server.Description </td>
        <td> server.IP </td>
        <td> server.ILO </td>
        <td> server.Rack </td>
        <td> server.Status </td>

        <td>
        </div>

                    <button type="button" class="btn btn-primary" data-toggle="modal" href="#delete-server-server.id" data-target="#DeleteItem">Delete <span class="glyphicon glyphicon-trash" </span></button>
                    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#DeleteItem">Edit <span class="glyphicon glyphicon-pencil" </span></button>
                    <div id ="DeleteItem" class="modal fade" role="document">
                        <div class="modal-dialog" id="delete-server-server.id">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title">Delete Confirmation</h5>
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                                </button>
                                    </div>
                                    <div class="modal-body">
                                    <form action="% url 'delete_post' server.id %" method="post">% csrf_token %
                                        <h6>Are you sure you want to delete  server.ServerName ?</h6>

                                        <input type="submit" class="btn btn-danger btn-md" value="Confirm delete"/>
                                            <button type="submit" class="btn btn-secondary" data-dismiss="modal">Cancel</button>

                                    </form>
                                </div>
                                </div>


                    </div>

        </td>
            </div>
    </tr>
            % endfor %
</tbody>

views.py

from django.shortcuts import render_to_response
from django.shortcuts import render, redirect
from django.template import RequestContext
from django.views.generic import TemplateView, UpdateView, DeleteView, CreateView
from DevOpsWeb.forms import HomeForm
from DevOpsWeb.models import serverlist
from django.core.urlresolvers import reverse_lazy

class HomeView(TemplateView):

    template_name = 'serverlist.html'

    def get(self, request):
        form = HomeForm()
        posts = serverlist.objects.all()
        args = 'form' : form, 'posts' : posts
        return render(request, self.template_name, args)

    def post(self,request):
        form = HomeForm(request.POST)
        posts = serverlist.objects.all()
        if form.is_valid(): # Checks if validation passed
            post = form.save(commit=False)
            post.save()
            text = form.cleaned_data['ServerName']
            form = HomeForm()
            return redirect('serverlist')
        args = 'form': form, 'text': text
        return render(request, self.template_name,args)


class PostDelete(DeleteView):
    model = serverlist
    success_url = reverse_lazy('serverlist')

urls.py -

from django.conf.urls import url, include
from DevOpsWeb.views import HomeView
from DevOpsWeb.views import PostDelete
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = [
    # Examples:
url(r'^$', HomeView.as_view(), name='serverlist'),

# Uncomment the admin/doc line below to enable admin documentation:
 url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
 url(r'^delete/(?P<pk>\d+)/$', PostDelete.as_view(), name="delete_post"),
 url(r'^django_popup_view_field/', include('django_popup_view_field.urls', namespace="django_popup_view_field")),
# Uncomment the next line to enable the admin:
 url(r'^admin/', include(admin.site.urls)),

]

【问题讨论】:

请显示您的删除视图代码 添加了 url 和视图文件 这有效吗? % url 'delete_post' pk=server.id % 我试过......同样......要求删除第一个项目并删除他而不是其他项目 好像html结构无效。这可能会导致您的问题。 td 必须是 tr 的直接子代。你在打开 td 后立即关闭了 div。 【参考方案1】:

尝试在你的删除按钮中更改data-target,这里是tbody的完整代码,你可以试试:

<tbody>
  % for server in posts %
  <tr>
    <td> server.ServerName </td>
    <td> server.Owner </td>
    <td> server.Project </td>
    <td> server.Description </td>
    <td> server.IP </td>
    <td> server.ILO </td>
    <td> server.Rack </td>
    <td> server.Status </td>
    <td>
      <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#Delserver.id">
        Delete <span class="glyphicon glyphicon-trash"</span>
      </button>
      <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#Delserver.id">
        Edit <span class="glyphicon glyphicon-pencil" </span>
      </button>
      <div id ="Delserver.id" class="modal fade" role="document">
      <div class="modal-dialog" id="delete-server-server.id">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title">Delete Confirmation</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <form action="% url 'delete_post' server.id %" method="post">
              % csrf_token %
              <h6>Are you sure you want to delete  server.ServerName ?</h6>
              <input type="submit" class="btn btn-danger btn-md" value="Confirm delete"/>
              <button type="submit" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
            </form>
          </div>
        </div>
      </div>
      </td>
  </tr>
  % endfor %
</tbody>

【讨论】:

我试过了.. 数据目标查找 div id.. 更改后确认删除停止工作。 @EilonAshkenazi 我更新了答案,删除了一些课程,但你可以试试tbody 完美。效果很好! 很高兴为您提供帮助!

以上是关于使用 Django 删除项目按钮仅删除第一项的主要内容,如果未能解决你的问题,请参考以下文章

成功删除、添加或编辑项目的 django 消息

Python 3 - 在链表中使用递归

使用按钮更新表中的值

在 django 模板中获取项目的值以进行循环

使用功能组件/挂钩从 React 中的数组中删除特定项目

Django:使用通用 DeleteView 删除选中的项目?