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 required arguments are model and object_id or slug.
The optional arguments are post_save_redirect, login_required, template_name, template_loader, extra_context, and context_processors.
The context variables are form, object, and extra_context.
The default template_name is app_label/model_name_form.html (such as People/Person_form.html).
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 FormIn 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:
|