Django: writing a view to delete an item with checkboxes
I need help writing a view that gets the POST data which then finds out which checkboxes have been checked, and then deletes the items from the database matched by id. Originally, This was a checkbox problem. In a edit order form, there are a list of items. Now I CAN remove the item using a bit of javascript but it does not get save because I need to remove it completly from my database. I tried using my edit order views to remove an item using the delete(), but that doe not work. There must be something else I am doing wrong but I don't know what.
delete_item = request.POST.get('delete_item', None)
if request.method == 'POST':
form = forms.OrderForm(request.POST, instance = order)
if form.is_valid() and s开发者_JS百科ave_item is not None:
form.save(True)
request.user.message_set.create(message = "The order has been updated successfully.")
return HttpResponse("<script language=\"javascript\" type=\"text/javascript\">window.opener.location = window.opener.location; window.close();</script>")
if status is not None and contact is not None and save_status is not None and delete_item is not None:
try:
for id in status_items:
item = models.StorageItem.objects.get(pk = id)
delete = item
delete.delete()
current_status = models.ItemStatusHistory(item = item, contact = contact, status = status,
user = request.user)
current_status.save()
except:
pass
request.user.message_set.create(message = "Status successfully changed for {0} items".format(len(status_items)))
{% for item in items %}
<tr class="items_table_row">
<td><input type="checkbox" name="item" value="{{item.pk}}" checked="checked"{% endif %}>
<td>{{item.tiptop_id}}</td><td>{{item.alternative_id}}</td><td>{{item.title}}</td><td>{{item.type}}</td><td>{{item.format}}</td>
<td><span id="{{item.pk}}" name="type">{{item.itemstatushistory_set.latest}}</span></td><td>{{item.itemstatushistory_set.latest.date.date|date:"d M Y"}}</td>
<td><a href="{% url tiptop.views.edit_item item.client.pk item.pk %}" onclick="return showAddAnotherPopup(this);">Edit</a></td>
</tr>
{% endfor %}
Given your template using checkboxes with name="item"
... .delete() should work.
Items.objects.filter(id__in=request.POST.getlist('items')).delete()
Are you getting an exception? Is the delete()
code ever running? Throw in a print statement.
You have "a ton of code", a lot of conditions we're not familiar with, and a try/except block so I just want to make sure the view is actually making it to the delete stage.
UPDATE: this is a rough piece of code to follow as a stranger to your code.
if status is not None and contact is not None and save_status is not None and delete_item is not None:
Why don't you just check for the existence of a specific button pressed instead?
# html
<input type="submit" name="save" value="Save Items" />
<input type="submit" name="delete" value="Delete Items" />
# view
if request.POST.get('delete'):
Items.objects.filter(id__in=request.POST.getlist('items')).delete()
elif request.POST.get('save'):
form = Form(request.POST)
# ... so on
精彩评论