Code

Opened 3 years ago

Closed 3 years ago

#16699 closed Bug (worksforme)

django.core.urlresolvers.reverse failed with complicated regexp

Reported by: Natim 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.

Attachments (0)

Change History (1)

comment:1 Changed 3 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.