Login

delete object from table in form

Author:
beckastar
Posted:
January 6, 2016
Language:
Python
Version:
Not specified
Tags:
views form table delete modelform
Score:
0 (after 0 ratings)

I had a difficult time understanding how to delete an item from a table within a template, using a modelform. I couldn't find a good example, so I wanted to post the code that ultimately worked.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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:**

<table>
	{% csrf_token %}
	<tr>	
	   <form action="" method="post">
	  {% csrf_token %}
		<td>{{form.item}}</td>
		<td>{{form.quantity}}</td>
		<td>{{form.needs_repairs}}</td>
		<td> <input class="table_button" type="submit" value="Add inventory" /></td>
	</form>
	</td></tr>
	    <thead>
		<tr>
		<td><p>Item?</p></td>
		<td><p>Quantity</p></td>
		<td><p>Needs Repairs</p></td>
		<td><p>Change</p></td>
		</tr>
		</thead>
		{% for item in inventory %}
			<tr class="datarow">
				<td>{{item.quantity}}</td>
				<td>{{item.needs_repairs}}</td>
				<td>
				<td>
		              <form action="/remove_items/" method="post">
		           {% csrf_token %}
				<input type="hidden" name="item_id" value="{{item.id}}" />
			       <input type="submit" value="delete item">
			</form>
	         </td>
              </td>
        </tr>
{% endfor %}
</table>

**urls.py**

    url(r'^inventory/', show_inventory_form, name="inventory"),
    url(r'^remove_items/', remove_items, name="remove_inventory"),

More like this

Comments

Please login first before commenting.