Previous Page Next Page

Using Additional URLconf Files

As you add more applications to your website, your URLconf file can become congested with all the URL patterns for the different applications. The best way to avoid this is to create an URLconf file for each application.

Django provides the include() function to add URLconf files to the URLconf file. The include() function accepts the location of the URLconf file using . (dot) syntax and takes the place of the view function in the URL pattern. For example, to include a new URLconf file, iFriends/Blog/urls.py, to be used when the /Blog/ URL is accessed, you would specify the following URL pattern:

(r'^Blog/', include('iFriends.Blog.urls')),

By the Way

Notice that the r'^Blog/' pattern doesn't include the $ character. This is because the URL dispatcher deletes the portion of the URL that is read before executing the include and passes only the remaining string. For example, if the preceding URL were Blog/index, only index would be passed in as the URL to the new URLconf file. The new URLconf file needs to have the trailing $ character specified in the pattern.


Django passes captured parameters through the include() function to other URLconf files. For example, if your root URLconf file contained the following pattern:

(r'^(?P<pID>\d+)/Blog/', include('iFriends.Blog.urls')),

the pID argument would still be captured and passed to the display() view function in the following pattern in the iFriends/Blog/urls.py URLconf file:

(r'^', 'iFriends.Blog.views.display'),

Django also allows you to pass a dictionary of extra options as an argument after the include() function. This passes the extra options to other URLconf files, which pass them to the view functions they invoke. For example, if your root URLconf file contains the following pattern:

(r'^Blog/', include('iFriends.Blog.urls'), {'pID':0}),

the dictionary containing the pID argument is still passed to the display() view function in the following pattern in the iFriends/Blog/urls.py URLconf file:

(r'^', 'iFriends.Blog.views.display'),

Try It Yourself: Add an URLconf File to Your Django Project

In this section, you will add a new URLconf file to the People application of the iFriends project. Follow these steps to create the new URLconf file and modify the root URLconf file to include the new one:

1.
Use an editor to create a file called iFriends/People/urls.py.

2.
Add the following code to the new urls.py file to handle the URL requests to People, as shown in Listing 6.4:

from django.conf.urls.defaults import *

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

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

By the Way

The code in the new URLconf file is similar to the code that was in the original URLconf file. However, remember that you need to remove the People/ portion of the pattern, because it is deleted before the include() function sends the URL to this file.

3.
Save the iFriends/People/urls.py file.

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

5.
Remove the following code that is used for the People/ 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),
)

6.
Add the following pattern, as shown in Listing 6.5, to point People/ URLs to the iFriends/People/urls.py URLconf file:

(r'^People/', include('iFriends.People.urls')),

7.
Save the iFriends/urls.py file.

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

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

Listing 6.4. Full Contents of the iFriends\People\urls.py File

from django.conf.urls.defaults import *

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

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

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

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^People/', include('iFriends.People.urls')),

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


Previous Page Next Page