I had trouble finding a good example for a deleting an item using a form in a table. Here's what worked: **models.py** class Inventory(models.Model): material = models.CharField(max_length=30) quantity = models.IntegerField(default=0) units = models.CharField(max_length=30) date = models.DateTimeField(auto_now_add=True, blank=True) def __str__(self): return '%s %s %s'%(self.material, self.quantity, self.units) **forms.py** class InventoryForm(ModelForm): class Meta: model = Inventory fields = '__all__' **views.py** def show_inventory_form(request): model = Inventory inventory = Inventory.objects.all() if request.method == "POST": form = InventoryForm(data = request.POST) if form.is_valid(): form.save() else: form = InventoryForm() context = RequestContext(request) return render_to_response('inventory.html', { 'form':form, 'inventory':inventory, }, RequestContext(request)) def remove_items(request): if request.method == 'POST': form = InventoryForm() inventory = Inventory.objects.all() item_id = int(request.POST.get('item_id')) item = Inventory.objects.get(id=item_id) item.delete() return render_to_response('inventory.html', { 'form':form, 'inventory':inventory, }, RequestContext(request)) **inventory.html:**
Item? |
Quantity |
Needs Repairs |
Change |
{{item.quantity}} | {{item.needs_repairs}} |