- Author:
- beckastar
- Posted:
- January 6, 2016
- Language:
- Python
- Version:
- Not specified
- 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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.