Previous Page Next Page

Using Generic Object-Update Views

The django.views.generic.create_update.update_object view renders a generic view that displays a form for updating objects. The update_object view requires a model argument and an object_id or slug argument. The view retrieves the object and uses it to create a Form object that is passed to the template in the context variable form.

By the Way

If the object has validation errors, update_object displays them.


The form is displayed if accessed with a GET request and the object is updated on a POST request.

Watch Out!

The update_object function actually modifies data in the database. You should use the login_required object to make certain that users are logged in before accessing the view.


The following code snippet shows an example of defining a generic update_object view in the URLconf file:

blog_info = {
    'model' : Blog,
    'post_save_redirect' : '/blog/list',
}
urlpatterns += patterns('',
    (r'^generic/blog_update/(?P<object_id>\d+)/$',
}

The following template code snippet shows an example of accessing the context variable form in a template to display the object being updated:

<form action="" method="post">
    <p><label for"id_title">Title:</label>{{ form.title}}</p>
    <p><label for"id_text">By:</label>{{ form.text }}</p>
<input type="submit" />

Try It Yourself: Use a Generic View to Implement an Object Update Form

In this section, you will use the update_object generic view to display a Quote object update form that updates Quote objects. Follow these steps to create a template and update the URLconf file to enable the view:

1.
Create and open a file called iFriends/templates/Quotes/quote_update.html in an editor.

2.
Add the following for form code to the file to generate an update form for the Quote object using the form context variable to access the text and by form elements:

<form action="" method="post">
    <p><label for"id_text">Text:</label>{{ form.text }}</p>
    <p><label for"id_by">By:</label>{{ form.by }}</p>
<input type="submit" value="Update"/>
</form>

3.
Add the rest of the code, shown in Listing 12.11, to extend the site base template and add the rest of the supporting HTML code for the view.

4.
Save the iFriends/templates/Quotes/quote_update.html file.

5.
Open the iFriends/urls.py file in an editor.

6.
Add the following lines of code, shown in Listing 12.12, to create a dictionary to use for the quote_update view that defines a custom template file and a post_save_redirect URL:

quote_update_info = {
    'model' : Quote,
    'template_name' : 'Quotes/quote_update.html',
    'post_save_redirect' : '/generic/quote_list',
}

7.
Add the following URL pattern to enable the generic quote_update view:

(r'^generic/quote_update/$',
  create_update.update_object, quote_update_info),

8.
Save the iFriends/urls.py file.

9.
Access the following URL in a browser, shown in Figure 12.1.

http://127.0.0.1:8000/generic/quote_list/

10.
Click the Update link for one of the quotes to access the quote_update view, shown in Figure 12.8.



Figure 12.8. Generic Quote object update form.


11.
Add data to the quote_update form, and click the Update button to update the Quote object.

12.
Verify that the quote_update view redirects you to the quote_list view and that the quote has been updated in the list, as shown in Figure 12.9.

Figure 12.9. Generic Quote object list form showing the quote entry updated by the quote_update view.




Listing 12.11. Full Contents of iFriends/templates/Quotes/quote_update.html

{% extends "iFriends_base.html" %}

{% block title %}People{% endblock %}
{% block content %}
<table width=100%>
<tr bgcolor="aabbcc"><td colspan="3">
<font size="5" color="white">Quote Update</font>
</td></tr>
<tr valign="top"><td width=30% bgcolor="aadddd">
<font size="5">
<form action="" method="post">
    <p><label for"id_text">Text:</label>{{ form.text }}</p>
    <p><label for"id_by">By:</label>{{ form.by }}</p>
<input type="submit" value="Update"/>
</form>
</font></td></tr>
{% endblock %}

Listing 12.12. Generic Section for Quote Objects in the iFriends/urls.py File

#Quote generic section
from django.views.generic import list_detail, create_update
from iFriends.Quotes.models import Quote

quote_list_info = {
    'queryset' : Quote.objects.all(),
    'allow_empty': True,
}

quote_add_info = {
    'model' : Quote,
    'template_name' : 'Quotes/quote_add.html',
    'post_save_redirect' : '/generic/quote_list',
}

quote_update_info = {
    'model' : Quote,
    'template_name' : 'Quotes/quote_update.html',
    'post_save_redirect' : '/generic/quote_list',
}

urlpatterns += patterns('',
    (r'^generic/quote_list/$',
      list_detail.object_list, quote_list_info),
    (r'^generic/quote_add/$',
      create_update.create_object, quote_add_info),
    (r'^generic/quote_update/(?P<object_id>\d+)/$',
      create_update.update_object, quote_update_info),
)


					  


Previous Page Next Page