Previous Page Next Page

Using View Prefixes

In the preceding section, you added three new URL patterns to your URLconf file. In each pattern, you specified the same view, iFriends.People.views.details. This is a lot of redundant code. This also can be problematic if you change the project's name, because you would need to change every URL pattern.

Django solves this problem by allowing you to specify a view prefix as the first argument to the patterns() function in the URLconf file. This can be used if all URL patterns being defined are located in the same view file. When a view prefix is added, you need to specify the view function in only the URL pattern. For example, if all URL patterns are located in the iFriends.Blogs.views file, you could use the following code to define the URL patterns for the index() and details() view functions:

urlpatterns = patterns('iFriends.Blogs.views',
    (r'^Blogs/$', 'index'),
    (r'^Blogs/Details/$', 'details'),
)

Sometimes you may need to have more than one prefix defined in the same URLconf file. You can use more than one prefix in the same URLconf file by calling the patterns() function multiple times and appending them to one another using the += operator, as shown in the following code snippet:

urlpatterns = patterns('iFriends.Blogs.views',
    (r'^Blogs/$', 'index'),
    (r'^Blogs/Details/$', 'details'),
)

urlpatterns += patterns('iFriends.People.views',
    (r'^People/$', 'index'),
    (r'^People/Details/$', 'details'),
)

Try It Yourself: Add a View Prefix to an URLconf File

In this section you will modify the URLconf file for the iFriends project to separate all the URL patterns that use the iFriends.People.views prefix to use a view prefix.

Follow these steps to open the iFriends/urls.py file and move the iFriends.People.views entries to their own patterns() function and specify a view prefix for them:

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

2.
Add the following code to append another patterns() function, as shown in Listing 6.3, specifying the iFriends.People.views prefix:

urlpatterns += patterns('iFriends.People.views',
)

3.
Move the URL patterns that use iFriends.People.views from the main patterns() function to the new patterns() function created in step 2:

(r'^People/$', 'iFriends.People.views.index'),
(r'^People/Info/$', 'iFriends.People.views.details'),
(r'^People/Info/(?P<pID>\d+)/$', 'iFriends.People.views.details'),
(r'^People/Contact/(?P<pID>\d+)/$', 'iFriends.People.views.details',
  details1),
(r'^People/Birthday/(?P<pID>\d+)/$', 'iFriends.People.views.details',
  details2),
(r'^People/Details/(?P<pID>\d+)/$', 'iFriends.People.views.details',
  details3),

4.
Modify the URL patterns to remove the iFriends.Blogs.views prefix to streamline the URLconf file, as shown in Listing 6.3:

(r'^People/$', 'index'),
(r'^People/Info/$', 'details'),
(r'^People/Info/(?P<pID>\d+)/$', 'details'),
(r'^People/Contact/(?P<pID>\d+)/$', 'details', details1),
(r'^People/Birthday/(?P<pID>\d+)/$', 'details', details2),
(r'^People/Details/(?P<pID>\d+)/$', 'details', details3),

5.
Save the iFriends/urls.py file.

6.
Test the URLConf file by accessing the following URL from a web browser:

http://127.0.0.1:8000/People/Info/1/

Listing 6.3. Full Contents of the iFriends\urls.py File

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    # Uncomment this for admin:
    (r'^admin/', include('django.contrib.admin.urls')),
)

details1 = {'opts':('name','email')}
details2 = {'opts':('name','birthday')}
details3 = {'opts':('name', 'desc', 'favoriteURL')}

urlpatterns += patterns('iFriends.People.views',
    (r'^People/$', 'index'),
    (r'^People/Info/$', 'details'),
    (r'^People/Info/(?P<pID>\d+)/$', 'details'),
    (r'^People/Contact/(?P<pID>\d+)/$', 'details', details1),
    (r'^People/Birthday/(?P<pID>\d+)/$', 'details', details2),
    (r'^People/Details/(?P<pID>\d+)/$', 'details', details3),
)


Previous Page Next Page