6.2.2 Reversing Regular Expression Patterns with Character Sets

Things get trickier with regular expression patterns that contain character sets, such as the organizer_tag_detail URL pattern, which has the regular expression pattern r'^tag/(?P<slug>[\w\-]+)/$'. When matching URL paths, the character set is useful because it allows both /tag/django/ and /tag/web/ to be matched. However, when reversing a URL, the character set becomes an unknown.

In this instance, URL reversal works like a template (or like a Mad Lib). Django expects us to provide the name of the URL pattern as well as any of the values that it needs to fill in the unknowns. To generate /tag/django/ from r'^tag/(?P<slug>[\w\-]+)/$', we must pass organizer_tag_detail and 'django' ...

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.