You code is not very clear,
you could make your changes to your view as below:
views
def inventory(request,id):
if request.method =='POST':
staff=Inventory.objects.get(id=id)
form=InventoryForm(request.POST,instance=staff)
if form.is_valid():
form.save()
return render_to_response('display_successful_message.html')
else:
staff=Inventory.objects.get(id=id)
form=InventoryForm(instance=staff)
return render_to_response('inventory.html')
Hope this helps.
Cheers,
Sum
On Fri, Dec 4, 2015 at 4:37 AM, Becka R. <[email protected]> wrote:
> 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>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/074cef57-c0d2-4f21-82d7-87912ec0e334%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/074cef57-c0d2-4f21-82d7-87912ec0e334%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAPCf-y6DJj9KPBRW7YgqL0ZOqFyVq51N%2B%3Dvq9vKxuV5pL4TOVA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.