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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access