Using URL References in the Templates

Obviously the model code can only return one URL for each object. The model class as such has no visibility on the functionality of the application; it is only designed to represent the data upon which the application operates. So usually a model instance returns the URL that is used to display the object, in other words a representation URL.

In our application we have multiple functions associated with the data entities, such as add, delete, and modify. Since we have a well-defined URL structure and all action "keywords" are appended at the end, we could use get_absolute_url on the object to get its base URL and then append the action word in the template, but this approach isn't proper, as the URL information would be contained in the URLConfig and each of the templates that uses it.

In the previous example we used the {{ object.get_absolute_url }} structure in the templates to refer to the URL. Django also has a URL resolver template tag, which is able to reference URLs by their names. You then need to pass an argument to it so it can match and generate the required URL:

{% url networkaddress-display address %}

Listing 4-7 shows a more verbose example of how to use the url tag.

Listing 4-7. An example of a URL resolver template tag

{% for address in addresses_list %}

<li><a href="{% url networkaddress-display address %}">

{{ address.address }}/{{ address.network_size }}</a> {% ifequal address.network_size 32 %}(host){% else %}(network){%-endifequal %}

{{ address.description }}

(<a href="{% url networkaddress-delete address %}">delete</a> | <a href="{% url networkaddress-modify address %}">modify</a>) </li> {% endfor %} </ul> {% else %}

{% ifequal parent.network_size 32 %} This is a node IP <ul>

<li>Description: {{ parent.description }}

( <a href="{% url networkaddress-modify parent %}">modify</a> )</li> </ul> {% else %}

No addresses or subnets in this range {% endifequal %} {% endif %}

All URL pattern names are defined in the URLConfig file, as shown in Listing 4-8.

Listing 4-8. Networkaddress URL patterns urlpatterns = patterns('', url(r'Anetworkaddress/$', views.networkaddress_display, name='networkaddress-displaytop'), url(r'Anetworkaddress/add/$', views.networkaddress_add, name='networkaddress-addtop'), url(r'Anetworkaddress/(?P<address>\d{l,3}\.\d{l,3}\.\d{l,3}\.\d{l,3}\/\d{l,2})/$', views.networkaddress_display, name='networkaddress-display'), url(r'Anetworkaddress/(?P<address>\d{l,3}\.\d{l,3}\.\d{l,3}\.\d{l,3}\/\d{l,2})/-delete/$', views.networkaddress_delete, name='networkaddress-delete'), url(r'Anetworkaddress/(?P<address>\d{l,3}\.\d{l,3}\.\d{l,3}\.\d{l,3}\/\d{l,2})/-add/$', views.networkaddress_add, name='networkaddress-add'), url(r'Anetworkaddress/(?P<address>\d{l,3}\.\d{l,3}\.\d{l,3}\.\d{l,3}\/\d{l,2})/^ modify/$', views.networkaddress_modify, name='networkaddress-modify'),

Finally, all URLs are decoupled and defined in one location—the URLConfig file. Whenever you choose to change them, you only need to do that in one place, and neither models, views, nor templates have to be modified.

Was this article helpful?

0 0

Post a comment