Editing Form Data

Created on Sept. 25, 2012, 7:26 p.m. by Hevok & updated by Hevok on May 2, 2013, 5:24 p.m.

In order to update and entry rather than to create a new one instance of this data entry needs to be retrieved from the database as first and passed to the form: ::

nano views.py
form django.http import HttpResponseRedirect

from forms import EntryForm
form data.models import Entry

def view(request, id):
    if request.method == 'POST':
        entry = Entry.objects.get(id__exact=id)
        form = EntryForm(request.POST, instance=Entry)

        if form.is_valid():
            entry = form.save(commit=False)

            return HttpResonseRedirect('/details/%s/' % (entry.id))

Tags: tutorial, coding, database, forms
Parent: Web Framework

Update entry (Admin) | See changes

Comment on This Data Unit