wagtaildjango-tagging

django-taggit 404 when using multiple word tag


I am implementing taggit tags to create a round-trip of pages. I have used the example code from the bakery-demo. Problem is that I need tags with multiple words, say two words like 'Design Thinking'. The back-end covers this neatly, creating a slug 'design-thinking'. But the listing-view does not work. It works with a single-word tag, but a double-word tag gives me a 404.

Tried if it works in the bakery-demo, but there the same problem arises. Which in a way is good, meaning I am not doing anything wrong. Just don't have the solution.

Has anyone solved this problem?


Solution

  • It appears that the regex for URL matching has been set to assume there will only be one word inside the tag slug.

    This means that the route would always fail for anything more than a single word tag.

    Original Code

    @route('^tags/$', name='tag_archive')
    @route('^tags/(\w+)/$', name='tag_archive')
    def tag_archive(self, request, tag=None):
        #...
    

    Explanation

    Note that the regex to match a URL of my-blog/tags/some-awesome-tag/ will only ever match a single word. A word in regex is represented by \w and does not contain spaces or dashes.

    However, a set of words or dashes will look like [\w-] the square brackets say to match any within these brackets and the dash means also match dashes.

    New Code

    The final new route decorator will look like the following:

    @route('^tags/$', name='tag_archive')
    @route('^tags/([\w-]+)/$', name='tag_archive') # this line changed
    def tag_archive(self, request, tag=None):
        #...
    

    Note: Submitted PR for fixing this issue in bakerydemo.