Thanks for your explanation, Alex. Unfortunately, "reverse('views.func')" always returns the first match for that function in urlpatterns.
My objective is to distinguish between two different URL patterns which both resolve to the same view function. Given your suggestion, I tried the following: >>> reverse('views.func', kwargs={'myname':'drink'}) >>> '/drink/' >>> reverse('views.func', kwargs={'myname':'exercise'}) >>> '/drink/' >>> reverse('views.func', kwargs={'myname':'eat'}) >>> '/drink/' >>> reverse('views.func', kwargs={'myname':'sleep'}) >>> '/drink/' So that solution does not work because the resolver apparently ignores the kwargs when presented with a function instead of a named URL. On Feb 13, 4:34 pm, Alex Koshelev <[EMAIL PROTECTED]> wrote: > Hi, graham > > Imagine that your urls2.py has some number of patterns. And then you > call `reverse('eat')` what of them have to be returned? So to get > proper result you have to write something like this > `reverse('views.func')` > > On 14 фев, 02:22, grahamu <[EMAIL PROTECTED]> wrote: > > > Lots of people correctly suggest using named url patterns and the {% > > url %} template tag to obtain a URL. The concept is great and follows > > the DRY principle. However, I believe that if a pattern has an > > "include(<filename>)" instead of a view function, the existing > > mechanism fails. An example of the problem, using three files in a > > standard Django project: > > > ----------file views.py---------- > > def func: > > pass > > ----------end file---------- > > > ----------file urls.py---------- > > from django.conf.urls.defaults import * > > from views import func > > urlpatterns = patterns('', > > (r'^drink/', 'views.func', {'myname':'drink'}, 'drink'), > > (r'^eat/', include('urls2'), {'myname':'eat'}, 'eat'), > > url(r'^exercise/', func, kwargs={'myname':'exercise'}, > > name='exercise'), > > url(r'^sleep/', include('urls2'), kwargs={'myname':'sleep'}, > > name='sleep'), > > ) > > ----------end file---------- > > > ----------file urls2.py---------- > > from django.conf.urls.defaults import * > > urlpatterns = patterns('', > > (r'^$', 'views.func'), > > ) > > ----------end file---------- > > > Invoking "manage.py shell", these are the results of calling reverse() > > (which I understand from the documentation works very much the same as > > {% url %}: > > > >>> from django.core.urlresolvers import reverse > > >>> reverse('drink') > > '/drink/' > > >>> reverse('eat') > > > Traceback (most recent call last): > > File "<console>", line 1, in ? > > File "c:\django\trunk\django\core\urlresolvers.py", line 297, in > > reverse > > return iri_to_uri(u'/' + get_resolver(urlconf).reverse(viewname, > > *args, **kw > > args)) > > File "c:\django\trunk\django\core\urlresolvers.py", line 284, in > > reverse > > raise NoReverseMatch > > NoReverseMatch>>> reverse('exercise') > > '/exercise/' > > >>> reverse('sleep') > > > Traceback (most recent call last): > > File "<console>", line 1, in ? > > File "c:\django\trunk\django\core\urlresolvers.py", line 297, in > > reverse > > return iri_to_uri(u'/' + get_resolver(urlconf).reverse(viewname, > > *args, **kw > > args)) > > File "c:\django\trunk\django\core\urlresolvers.py", line 284, in > > reverse > > raise NoReverseMatch > > NoReverseMatch > > > Is there a workaround for this issue? Am I doing something > > incorrectly? Should I file a ticket? > > Graham --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---