Hi,
I've been tryign to understand this for a while, and am pretty stuck. I've googled extensively.
My form currently allows me to add items to teh datatable.
I want to be able to, in the same template, edit each row. I put a button in each table row, and I want to get the pk for the item in that row, and then edit that item. I don't understand how this is done. I've tried declaring an instance, but I got lost in the fog after that. Thanks so much. Code is below.
I have a basic modelform:
models:
class Inventory(models.Model):
item = models.CharField(max_length=20, blank=True)
needs_repairs = models.BooleanField(default=True)
date = models.DateTimeField(auto_now_add=True, blank=True)
def __str__(self):
return '%s %s'%(self.item, self.needs_repairs)
views
def inventory(request):
model = Inventory
stuff = Inventory.objects.all()
form = InventoryForm(data = request.POST)
if request.method == "POST":
if form.is_valid():
obj = form.save(commit=False)
obj.save()
else:
form = InventoryForm()
context = RequestContext(request)
return render_to_response('inventory.html', {
'form':form, 'stuff':stuff
}, RequestContext(request))
forms
class InventoryForm(ModelForm):
class Meta:
model = Inventory
fields = '__all__'
template
<div>
<div class="formfloatleft">
<h2>Truck Inventory </h2>
<div class="tablewrap">
<table class="gradienttable">
<thead>
<tr>
<th><p>Whatcha got?</p></th>
<th><p>Needs Repairs</p></th>
<th><p>Change</p></th>
</tr>
</thead>
<div class="datatables">
{% for things in stuff %}
<tr class="datarow">
<td>{{things.item}}</td>
<td>{{things.needs_repairs}}</td>
<td>
<form method="post">
{% csrf_token %}
<input type="submit" value="edit or remove me">
</form>
</td>
</tr>
{% endfor %}
</div>
</table>
</div>
</div>
<div class='formfloatright' >
<h2>Add stuff</h2>
<form action="" method="post">
{% csrf_token %}
<ul>
{{ form.as_ul}}
</ul>
</ul>
<input type="submit" value="Save" />
</form>
</div>
</div>