try this: reverse("mysites.views.index")
2011/7/16 Squeesh <[email protected]>
> 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
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.