Code

Opened 4 years ago

Closed 3 years ago

#12950 closed (duplicate)

urlresolvers.reverse returns '/$' when including namespaced '^$' pattern at root '^$'

Reported by: ungenio Owned by: nobody
Component: Core (Other) Version: master
Severity: Keywords: urlresolvers reverse namespace
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

In project urls.py:

incpatterns = patterns('',
    url(r'^$', myapp.views.index, name='index'),
)
urlpatterns = patterns('',
    (r'^$', include(incpatterns, namespace='myapp')),
)
>>> reverse('myapp:index')
'/$'

It correctly returns '/' if the included pattern isn't namespaced.

Attachments (2)

test-reverse_namespaced_url.patch (1.3 KB) - added by ungenio 3 years ago.
reverse_namespaced_url.patch (1.1 KB) - added by ungenio 3 years ago.
Fix parsing url regex patterns when including namespaced urls.

Download all attachments as: .zip

Change History (14)

comment:1 Changed 4 years ago by Alex

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

Your resolver is broken, you can't include at something ending with a $.

comment:2 Changed 4 years ago by ungenio

I figured as much, but was confused by it working when a namespace wasn't specified.

Thanks!

comment:3 Changed 4 years ago by SmileyChris

  • Resolution invalid deleted
  • Status changed from closed to reopened
  • Triage Stage changed from Unreviewed to Accepted

No, there's definitely something fishy going on.

comment:4 Changed 4 years ago by SmileyChris

Normal:

incpatterns = patterns('',
    url(r'^$', 'views.front', name='index'),
)
urlpatterns = patterns('views',
    (r'test?/', include(incpatterns)),
)

>>> reverse('index')
'/tes/

Namespaced:

incpatterns = patterns('',
    url(r'^$', 'views.front', name='index'),
)
urlpatterns = patterns('views',
    (r'test?/', include(incpatterns, namespace='myapp')),
)

>>> reverse('myapp:index')
'/test?/

comment:5 Changed 3 years ago by v1v3kn

  • Resolution set to invalid
  • Status changed from reopened to closed

@SmileyChris

This is not really a bug, this is due to the way the '?' quantifier works.

'?' matches the character preceding it zero or one time(s).
Eg: the regex pattern 'cows' matches both 'cow' and 'cows'.

And as Alex said earlier, you can't include anything after a '$'.

Version 0, edited 3 years ago by v1v3kn (next)

comment:6 Changed 3 years ago by SmileyChris

  • Resolution invalid deleted
  • Status changed from closed to reopened

Perhaps you misread my example code. I didn't include anything after a '$'.

And I understand how quantifiers work. The issue here is that a namespaced app isn't processing the regular expression question mark as expected.

I haven't actually retested my code, but your reasoning for closing doesn't show that you did either so I'll reopen.

comment:7 Changed 3 years ago by ungenio

  • Has patch set
  • Version changed from 1.2-beta to SVN

Regex for namespaced RegexURLResolver wasn't getting processed.

I believe I have found a solution. Please see patch.

comment:8 Changed 3 years ago by lrekucki

  • Needs tests set

comment:9 Changed 3 years ago by ungenio

  • Patch needs improvement set

Hadn't run tests on my patch. They break.

Will do some more digging and add a regression test for this.

Changed 3 years ago by ungenio

comment:10 Changed 3 years ago by ungenio

  • Needs tests unset

Added a simple regression test. Will see if I can figure out how to resolve.

Changed 3 years ago by ungenio

Fix parsing url regex patterns when including namespaced urls.

comment:11 Changed 3 years ago by ungenio

  • Patch needs improvement unset

New patch passes tests.

What patch does is copy namespaced resolver into non-namespace resolver, then wraps with root resolver so that everything works.

comment:12 Changed 3 years ago by carljm

  • Resolution set to duplicate
  • Status changed from reopened to closed

I believe this is a duplicate of #11559.

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.