Previous Page Next Page

Adding Links with the url Tag

Django provides the url tag to help you add links to your web pages. If possible, it is best to avoid hard-coded links. If you modify the URLconf file and thus change the URL pattern, you might wind up needing to change every place in your code that you hard-coded the link.

To solve this problem, the url tag allows you to specify the view function, including arguments, directly. The url tag results in an absolute URL being placed in the rendered HTML document.

The first argument inside the url tag is a path to a view function, such as the path to the details() function in the views.py file of the People application in the iFriends project:

iFriends.People.views.details

Additional arguments are optional and should be separated by commas with no spaces. Arguments are considered positional unless you specify a keyword=value. For example:

{url mySite.arch.views.list arg1,arg2,year=2008 %}

By the Way

If you are using named URL patterns, you can refer to the name of the pattern in the url tag instead of using the path to the view.


The arguments specified in the url tag must correspond to what is defined in the URLconf file. If they do not, no URL is returned.

Watch Out!

The url tag searches for the first match in the URLconf file. If multiple patterns are pointing to the same view, either use named patterns, or make certain that the first one in the list is the one you want.


Try It Yourself: Add Dynamic Links to a Template

In this section, you will modify the person_index.html file you created earlier in this hour to add dynamic links to the details page for each person in the list. Follow these steps to add dynamic links that refer to the view function directly to the index view of the People application:

1.
Open the iFriends/templates/People/person_index.html file in an editor.

2.
Remove the following line of code from the file:

<li> {{ p.name }}</li>

3.
Add the following code to the file, as shown in Listing 8.4, to add links to the details() view function and pass the Person.id argument to the view:

<li>
<a href="{% url iFriends.People.views.details p.id %}">{{ p.name }}</a>
</li>

4.
Save the file.

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

6.
Comment out the following line by putting a # in front of it so that the template parser won't pick it up when parsing the url tag from step 3.

#(r'^Info/$', 'details'),

7.
Save the file.

8.
Access the following URL in a web browser to bring up the index view, shown in Figure 8.3, and verify that the names are now links:

http://127.0.0.1:8000/People/

Figure 8.3. Web index web page generated with links to the details() view of the People application.


9.
Click one of the person links to verify that the links are working correctly. That person's detail view should be loaded, as shown in Figure 8.4.

Figure 8.4. Web details web page view of the People application.


Listing 8.4. Full Contents of iFriends/templates/People/person_index.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">Personal List</font>
</td></tr>
<tr valign="top"><td width=30% bgcolor="aaaaaa"><font color="white" size="5">
{% for p in pList %}
    <li>
    <a href="{% url iFriends.People.views.details p.id %}">{{ p.name }}</a>
    </li>
{% endfor %}
</font></td></tr>
{% endblock %}


					  


Previous Page Next Page