Previous Page Next Page

Using Generic Object-Creation Views

The django.views.generic.create_update.create_object view renders a generic view that displays a form for creating objects. The create_object view requires a model argument containing the model of the class you want to create. A Form object is generated for the model and is passed to the template in the context variable form.

By the Way

If the object has validation errors, create_object displays them.


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

Watch Out!

The create_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 create_object view in the URLconf file:

blog_info = {
    'model' : Blog,
    'post_save_redirect' : '/blog/list',
}
urlpatterns += patterns('',
    (r'^generic/blog_add/$',
      create_update.create_object, blog_info),

The following template code snippet shows an example of accessing the context variable form in a template:

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

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

In this section, you will use the create_object generic view to display a Quote object creation form and create 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_add.html in an editor.

2.
Add the following for form code to the file to generate a creation 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="Add"/>
</form>

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

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

6.
Modify the following line of code in Listing 12.10 to import the create_update view:

from django.views.generic import list_detail, create_update

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

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

8.
Add the following URL pattern to enable the generic quote_add view:

(r'^generic/quote_add/$',
  create_update.create_object, quote_add_info),

9.
Save the iFriends/urls.py file.

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

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

11.
Click the Add link to access the quote_add view, shown in Figure 12.6.

Figure 12.6. Generic Quote object creation form.


12.
Add data to the quote_add form, and click the Add button to create the new Quote object.

13.
Verify that the quote_add view redirects you to the quote_list view and that the new quote has been added to the list, as shown in Figure 12.7.



Figure 12.7. Generic Quote object list form showing the new quote entry added by the quote_add view.




Listing 12.9. Full Contents of iFriends/templates/Quotes/quote_add.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 Add</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="Add"/>
</form>
</font></td></tr>
{% endblock %}

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

urlpatterns += patterns('',
    (r'^generic/quote_list/$',
      list_detail.object_list, quote_list_info),
    (r'^generic/quote_add/$',
      create_update.create_object, quote_add_info),
)


Previous Page Next Page