6.2.1 Reversing URL Patterns

If we take a regular expression pattern without character sets or groups (named or otherwise), then reversal is simple. Our organizer_tag_list URL pattern has r'^tag/$' for its regular expression pattern. The URL path /tag/ is the only string that matches this regular expression pattern and therefore the string we want to generate.

As with everything else, Django makes our lives easy by supplying a tool to do exactly what we want, helpfully called reverse(). We can simply import the reverse() function from django.core.urlresolvers. We then pass reverse() the name attribute of a URL pattern. To test this functionality, we step into the Python interpreter (or IPython) with ./manager.py shell, as shown in Example 6.1 ...

Get Django Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.