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 FileIn 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:
Listing 6.3. Full Contents of the iFriends\urls.py File
|