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

时间:2017-10-09 13:40:42

标签: html mysql django database web

所以我一直在DJANGO建立一个服务器表(IP,所有者等)的网站。

我正在使用mysql。

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

有一行 -

我尝试将server.id,{{server.id}}放入..没有任何效果。 有没有人有任何想法?

P.S 当我在确认中询问你是否要删除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)),

1 个答案:

答案 0 :(得分:0)

尝试更改删除按钮中的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="#Del{{server.id}}">
        Delete <span class="glyphicon glyphicon-trash"</span>
      </button>
      <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#Del{{server.id}}">
        Edit <span class="glyphicon glyphicon-pencil" </span>
      </button>
      <div id ="Del{{server.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>