Previous Page Next Page

Using Generic Object-Deletion Views

The django.views.generic.create_update.delete_object view renders a generic view that displays a form for deleting objects. The delete_object view requires a model, an object_id, and a post_delete_redirect object. The view retrieves the object and uses it to create a Form object that is passed to the template in the context variable form.

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

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

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

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

<form action="" method="post">
    <p>{{ object.title }}</p>
    <p> -{{ object.text }}</p>
<input type="submit" />
</form>

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

In this section you will use the delete_object generic view to display a Quote object deletion form that deletes 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_delete.html in an editor.

2.
Add the following form code to the file to generate a deletion form for the Quote object. This code uses the object context variable to access the text and by form elements:

<form action="" method="post">
    <p>{{ object.text }}</p>
    <p> -{{ object.by }}</p>
<input type="submit" value="Delete"/>
</form>

3.
Add the rest of the code, shown in Listing 12.13, 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_delete.html file.

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

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

quote_delete_info = {
    'model' : Quote,
    'template_name' : 'Quotes/quote_delete.html',
    'post_delete_redirect' : '/generic/quote_list',
}

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

(r'^generic/quote_delete/(?P<object_id>\d+)/$',
  create_update.delete_object, quote_delete_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 Delete link for one of the quotes to access the quote_delete view, shown in Figure 12.10.

Figure 12.10. Generic Quote object delete form.


11.
Add data to the quote_delete form, and click the Delete button to delete the Quote object.

12.
Verify that the quote_delete view redirects you to the quote_list view and that the quote has been removed from the list, shown in Figure 12.11.



Figure 12.11. Generic Quote object list form showing that the quote entry has been deleted by the quote_delete view.




Listing 12.13. Full Contents of iFriends/templates/Quotes/quote_delete.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 Delete</font>
</td></tr>
<tr valign="top"><td width=30% bgcolor="aadddd">
<font color="white" size="5">
<form action="" method="post">
    <p>{{ object.text }}</p>
    <p> -{{ object.by }}</p>
<input type="submit" value="Delete"/>
</form>
</font></td></tr>
{% endblock %}

Listing 12.14. 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',
}

quote_delete_info = {
    'model' : Quote,
    'template_name' : 'Quotes/quote_delete.html',
    'post_delete_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),
    (r'^generic/quote_delete/(?P<object_id>\d+)/$',
      create_update.delete_object, quote_delete_info),
)


					  


Previous Page Next Page