2011-02-08 131 views
0

我有一個項目列表。每個項目都有一個複選框。我希望能夠使用刪除所有檢查項目的按鈕(被勾選的項目)來刪除項目。我有一些jscript可以完成一半的工作,但是從我的數據庫中刪除這個項目已經證明了很多問題。當我按下刪除按鈕時,它將刪除該項目。但是當我再次打開表單時,該項目再次返回。似乎無法刪除對象:Django

這是我的看法。

def edit_order(request, order_no): 
#some code 
items = models.StorageItem.objects.filter(orderservicelist__order__pk = order.pk) 
#some more code including if POST 
    item = models.StorageItem.objects.get(pk = id) 
     if request.POST.get('delete'): 
      item.delete() 

而且我的模板

{% extends "base_popup.html" %} 


    {% block title %} 
      {{title}} 
    {% endblock %} 

    {% block script %} 

    <script type="text/javascript" src="{{MEDIA_URL}}ui/ui.datepicker.min.js"></script> 
    <script type="text/javascript"> 
      $(function(){ 
        $("#id_required_date").datepicker({dateFormat:"dd/mm/yy"}); 
        $(":checkbox").css("width","auto"); 
      }); 
      $(function(){ 
        $("#check_all").click(function(){ 
          if(this.checked ==true) 
              $("tbody :checkbox").each(function(){ 
                this.checked=true; 
              }); 
            else 
              $("tbody :checkbox").each(function(){ 
                this.checked=false; 
              }); 
        }); 
         }); 
    </script> 
    <script> 
    function hideCheckedRows() { 
     var checkboxes = document.getElementsByName("item"); 
     var checkboxes_to_remove = new Array(); 
     var count = 0; 
     for (var i = 0; i < checkboxes.length; i++) { 
      if (checkboxes[i].checked == true) { 
       checkboxes_to_remove[count++] = checkboxes[i]; 
      } 
     } 
     for (var i = 0; i < checkboxes_to_remove.length; i++) { 
      cbx = checkboxes_to_remove[i]; 
      // parentNode.parentNode.parentNode is the <tr> 
      // parentNode.parentNode is the <td> containing the checkbox 
      cbx.parentNode.parentNode.parentNode.removeChild(cbx.parentNode.parentNode); 
     } 
    } 
    </script> 


    {% endblock %} 
    {% block content %} 
        <div id="location_header">{{title}}</div> 
    <div id="form_container"> 
    <form action="." method="post"> 
      <fieldset class="model"> 

        <p> 
          <span style="font-weight:bold;font-size:14px">Contact : {{order.contact}}</span> 
        </p> 
        <p> 
          <span style="font-weight:bold;font-size:14px">Cost : {{order.cost}}</span> 
        </p> 
        {{ form.as_p }} 
      </fieldset> 
      <fieldset class="model"> 
        <legend>Items</legend> 
        <table id="items_table"> 

          <thead> 
            <tr> 
              <td><input type="checkbox" id="check_all" checked="checked"></td> 
              <td>Tiptop no</td><td>Client no</td><td>Title</td><td>Item type</td> 
              <td>Format</td> 
            </tr> 
          </thead> 
          <tbody> 
        {% for item in items %} 
          <tr> 
          <td><input type="checkbox" name="item" value="{{item.pk}}" checked="checked"></td> 
          <td>{{item.tiptop_id}}</td><td>{{item.alternative_id}}</td><td>{{item.title}}</td> 
          <td>{{item.type}}</td><td>{{item.format}}</td> 

          </tr> 
        {% endfor %} 
        </tbody> 
        </table> 
        <p> 
          <form method="post" action="help"> 
          <table width="60%"> 
            <tr> 
              <td> 
                <select name="contact_id"> 
                {% for contact in order.contact.client.contact_set.all %} 
                  <option value="{{contact.pk}}">{{contact}}</option> 
                {% endfor %} 
                </select> 
              </td> 
              <td> 
                <select name="status_id"> 
                {% for status in status_list %} 
                  <option value="{{status.pk}}">{{status}}</option> 
                {% endfor %} 
                </select> 
              </td> 
              <td><input type="submit" name="save_status" value="set status for selected items"></td> 
            </tr> 
          </table> 
        </form> 
        </p> 
      </fieldset> 

      <div id="form_footer"> 
        <span style="font-size:10px;font-weight:bold;margin-right:10px"> 
        </span> 
        <input type="button" value="Add item" onclick="window.location.href='{% url tiptop.views.client_items name.pk %}'" /> 
        <input type="submit" name="save_item" value="Save" onclick="validate_item(this.form)"> 
        <input type="button" name="delete" value="Delete Items" onclick="hideCheckedRows()"> 
      </div> 
    </form> 
    </div> 
    {% endblock %} 
+0

視圖如何被調用? – 2011-02-08 11:12:10

+0

哦,抱起來,讓我編輯我的模板。更新。有一個項目清單。顯示他們的細節。 – Shehzad009 2011-02-08 11:15:18

回答

2

您的問題是request.POST從未包含delete關鍵。

type="button"元素需要type="submit"才能提交表格。

您只是隱藏了元素hideCheckedRows()