Search code examples
regexdjangourlattributeerror

Getting "AttributeError: 'str' object has no attribute 'regex'" in Django urls.reverse


As in the title, I'm getting

AttributeError: 'str' object has no attribute 'regex'

I've seen many valid answers to this, but none of them applied to my code as they identified issues in the urls.py file. My issue is in the views.py file.

I get the exception when using reverse to generate a redirect URL:

HttpResponseRedirect(reverse('changetracker:detail', args))

When I use 'changetracker:detail' in other functions, I don't get the exception.


Solution

  • I'm answering this to share knowledge as I didn't see this particular root cause identified already.

    The problem turned out to be that I should be using a keyword argument 'args=args' to pass in URL arguments, not a positional argument:

    HttpResponseRedirect(reverse('changetracker:detail', args=args))
    

    Using the positional argument (position 2) caused reverse to use that argument as the URL list and thus raise the AttributeError.