What You'll Learn in This Hour |
|
In Hour 4, "Creating the Initial Views," you learned how to set up the initial views in the URLconf file. In this hour, we will expand on that topic to include more advanced configuration options for views. When designing your website, the more complete your URLconf file configuration for web page views is, the easier it is to keep your code in the view cohesive and clean.
The following sections describe some advanced configuration options that you can make in the URLconf and views.py files.
The syntax for URL patterns in the URLconf file allows you to add additional options that will be passed as additional arguments to the view function. This is different from capturing arguments inside the URL, which we have already discussed.
Django allows you to specify a Python dictionary containing arguments that will be passed to the view function by the URL dispatcher. For example, the following URL pattern passes a simple dictionary:
(r'^People/BlogArchive/$', 'iFriends.Blogs.views.index', {'label':'Archive'}),
When this URL pattern is accessed, the URL dispatcher calls the index() view function:
index(request, label='Archive')
Just as with arguments captured from the URL, you need to add the arguments to the view function definition in the views.py file.
You can also capture arguments from the URL along with passing extra options in a dictionary. For example, the following URL pattern captures the year argument and passes a simple dictionary containing a label argument:
(r'^People/BlogArchive/(?P<year>\d{4})/$', 'iFriends.Blogs.views.index', {'label':'Archive'}),
When this URL pattern is accessed, the URL dispatcher calls the index() view function:
index(request, year='2008, label='Archive')
Watch Out!
It is possible to specify the same argument name in both the captured arguments and the extra options dictionary. If this occurs, the value in the extra options dictionary is used instead of the value that was captured from the URL.
The ability to pass options to view functions provides much more control over the views from the URLconf file. This gives you much more flexibility in the URL patterns that your website will handle. The captured arguments and the extra options dictionary also make the website easier to design and manage, because much of the control is contained in the URLconf file.
Try It Yourself: Pass Extra Options to the View Function from the URLconf FileThe best way to help you understand how useful the extra options dictionary can be is to apply it to the iFriends website that we have been working with. Currently, we have two views—an index() view that displays a list of Person objects, and a details() view that displays details of a specific Person object. In this section, you will add functionality to the details() view that will make it possible to use the URLconf file to determine what details will be displayed. Follow these steps to modify the views.py and urls.py files of the iFriends project:
Listing 6.1. Full Contents of the iFriends\urls.py File
Listing 6.2. Full Contents of the iFriends\People\views.py File
|