django.core.urlresolvers.reverse capturing group before include behavior

29 views
Skip to first unread message

Squeesh

unread,
Jul 15, 2011, 4:01:12 PM7/15/11
to Django users
I have run into an issue using reverse() to lookup named urls with
capturing groups before an include().

Code:

--------mysite/urls.py--------
from django.conf.urls.defaults import *

urlpatterns = patterns('',
url(r'^$', 'views.index',
name='root'),
url(r'^(?P<some_slug>\w+)/test/', include('test.urls',
namespace='test')),
)
--------end mysite/urls.py--------


--------mysite/test/urls.py--------
from django.conf.urls.defaults import *

urlpatterns = patterns('',
url(r'^$', 'test.views.index',
name='index'),
)
--------end mysite/test/urls.py--------


--------mysite/views.py--------
from django.core.urlresolvers import reverse
from django.http import HttpResponse

def index(request):
# Works: returns '/(?P<some_slug>\w+)/test/'
test_url = reverse('test:index')

# NoReverseMatch: Reverse for 'index' with arguments '('slug',)'
# and keyword arguments '{}' not found.
test_url = reverse('test:index', args=['slug'])

# NoReverseMatch: Reverse for 'index' with arguments '()'
# and keyword arguments '{'some_slug': 'slug'}' not found.
test_url = reverse('test:index', kwargs={'some_slug': 'slug'})

# Desired output is '/slug/test/'

return HttpResponse("""
root index<br/>
<a href="{test_url}">Test</a><br/>
""".format(test_url=test_url))
--------end mysite/views.py--------


--------mysite/test/views.py--------
from django.http import HttpResponse

def index(request, some_slug):
return HttpResponse('test index: %s' % some_slug)
--------end mysite/test/views.py--------


reverse('test:index') will only resolve if you don't pass it any
arguments. And then the result is the original regex... The desired
behavior is for it to either accept args or kwargs of 'slug' and
return '/slug/test/'.

Is there a setting that I am missing to allow for this behavior? Is
this a known bug?

Any information would be greatly appreciated.


Derek

Jian Chang

unread,
Jul 15, 2011, 9:40:56 PM7/15/11
to django...@googlegroups.com
try this: reverse("mysites.views.index")

2011/7/16 Squeesh <squ...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Squeesh

unread,
Jul 18, 2011, 9:03:22 AM7/18/11
to django...@googlegroups.com
Thanks Chang.Jian,


Goth

test_url = reverse('test.views.index', args=['slug'])

and

test_url = reverse('test.views.index', kwargs={'some_slug': 'slug'})

work as expected...

reverse('test:index', args=['slug']) would be the desired syntax, but the other will work fine as a work around. Anyone know if this is a known issue with the dev team? My Google searches didn't come up with much.

Squeesh

unread,
Jul 18, 2011, 9:04:06 AM7/18/11
to django...@googlegroups.com
Both*
Reply all
Reply to author
Forward
0 new messages