Opened 13 years ago

Closed 13 years ago

#16699 closed Bug (worksforme)

django.core.urlresolvers.reverse failed with complicated regexp

Reported by: Rémy Hubscher Owned by: nobody
Component: Core (Other) Version: 1.3
Severity: Normal Keywords: urlresolver
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I was using this rule in my project :

url(r'^qr/(?P<alias>[a-z0-9-]+)/((?P<size>\d+)/)?$', qrimage, name="qrimage"),

And it the use of reverse('qrimage', args['qrkit', 600]) raise

Caught NoReverseMatch while rendering: Reverse for 'qrimage' with arguments '(u'qrkit', 600)' and keyword arguments '{}' not found.

To solve the problem I used :

url(r'^qr/(?P<alias>[a-z0-9-]+)/(?P<size>\d+)/$', qrimage, name="qrimage"),

But then it is not really the same behaviour than the one I wanted.

Change History (1)

comment:1 by Aymeric Augustin, 13 years ago

Resolution: worksforme
Status: newclosed

The reverse function doesn't work in all situations, see https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse:

The reverse() function can reverse a large variety of regular expression patterns for URLs, but not every possible one.

In your example, how could reverse() decide to add or not to add the trailing slash?

If I understand correctly, you're trying to make your URLs equivalent with and without the trailing slash. However, it's considered a good practice to have only one URL for a given resource, for semantic and SEO reasons. The common practice (and default behavior of Django) is to always use a trailing slash — see https://docs.djangoproject.com/en/dev/ref/settings/#append-slash.

For now, I will close this ticket because it's a known and documented limitation, and the problem doesn't occur when you follow the recommended best practices. However, if you write a patch to improve the implementation of reverse(), please reopen this ticket.

Note: See TracTickets for help on using tickets.
Back to Top