Previous Page Next Page

Updating Data in the Database

When you have finished validating data in the Form, the data can be saved in the database. To save the data in the Form to the database, use the save() function of the Form instance. The save() function of the Form object works the same way as the save() function of Model objects that was discussed in Hour 5, "Using Data from the Database in Views."

The save() function preprocesses the data in the Form, prepares the data to be inserted into the database, and inserts the data into the database. The Form save() function uses the same database table, so as long as the primary key matches, the object in the database is updated.

Try It Yourself: Save Form Data to the Database

In this section, you will add save functionality to the person_form() view function so that when the update button is clicked, the data actually gets saved to the database. Follow these steps to call the save() function on the PersonForm object created in person_form():

1.
Open the iFriends/Person/views.py file in an editor.

2.
Add the following try block to the POST handler in the person_form() view, shown in Listing 11.4, to save the Form data to the database or to adjust the message with a database error:

try:
      f.save()
      message += ' Updated.'
except:
      message = ' Database Error.'

3.
Save the iFriends/Person/views.py file.

4.
Open the iFriends/templates/People/person_details.html file in an editor.

5.
Add the following line of code to the first row of the HTML table, shown in Listing 11.4, in the person_details.html template file to add a dynamic link to the person_form() view:

<a href="{% url iFriends.People.views.person_form pID=p.id %}">[Edit]</a>

6.
Save the iFriends/templates/People/person_details.html file.

7.
Access the following URL in a web browser to bring up the Person details() view, shown in Figure 11.5:

http://127.0.0.1:8000/Person/Info/1/

Figure 11.5. Person details() view with updated values from the Person form view.


8.
Click the new Edit link to bring up the person_form() view, shown in Figure 11.5.

9.
Modify the form with valid data, and click the update button.

10.
Access the following URL again in a web browser to bring up the Person details() view, shown in Figure 11.5, and verify that the data has been updated:

http://127.0.0.1:8000/Person/Info/1/

Listing 11.4. Full Contents of the person_form() View in iFriends/People/views.py

def person_form(request, pID='0'):
    #default
    PersonForm = forms.form_for_model(Person)
    f = PersonForm()
    message = 'Unknown Request'

    p = get_object_or_404(Person, pk=pID)

    if request.method == 'GET':
        PersonForm = forms.form_for_instance(p)
        f = PersonForm()
        message = 'Editing person %s ' % p.name

    if request.method == 'POST':
        if request.POST['submit'] == 'update':
            message = 'Update Request for %s.' % p.name
            PersonForm = forms.form_for_instance(p)
            f = PersonForm(request.POST.copy())
            if f.is_valid():
                try:
                    f.save()
                    message += ' Updated.'
                except:
                    message = ' Database Error.'
            else:
                message += ' Invalid.'

    return render_to_response(
        'People/person_form.html',
         {'pForm':f,
          'message': message})


					  


Previous Page Next Page