#4915: A smarter resolver: try to disambiguate URL patterns
-------------------------------+--------------------------------------------
Reporter: [EMAIL PROTECTED] | Owner: adrian
Status: new | Component: Core framework
Version: SVN | Resolution:
Keywords: | Stage: Unreviewed
Has_patch: 0 | Needs_docs: 0
Needs_tests: 0 | Needs_better_patch: 0
-------------------------------+--------------------------------------------
Comment (by [EMAIL PROTECTED]):
I guess an explanation of the situation I'm trying to solve here ''would''
probably be apropos.
I've got a view function 'account.views.profile' with the signature
{{{
def profile(request, user=None):
}}}
which uses None as a placeholder for "the current user". To go along with
this, I've also got the URL patterns
{{{
(r'^/profile/$', 'account.views.profile'),
(r'^/profile/(?P<user>\d+)/', 'account.views.profile'),
}}}
Using the current reverse resolver, however, this ends up not working
correctly. Depending on which pattern is first in the URL configuration,
either
{{{
django.core.urlresolvers.reverse('account.views.profile', kwargs={'user':
12345})
}}}
will (incorrectly) return '/profile/', or
{{{
django.core.urlresolvers.reverse('account.views.profile')
}}}
will throw NoReverseMatch. This is primarily because the current reverse
resolver can only store one URL pattern per view function.
What I'm after with this patch is rectifying that situation at least
partially. I haven't done away with named patterns entirely - what I've
done is a little more subtle:
* RegexURLResolver._get_reverse_dict now creates a dictionary mapping view
functions to '''lists''' of URL patterns which use those functions, rather
than simply "crushing" duplicates.
* Using this new information, RegexURLResolver.reverse tests its results
by resolving them (this could probably be optimized!) and making sure that
the view function, and arguments match the view function and args/kwargs
which were passed into RegexURLResolver.reverse().
The critical difference here is that this makes RegexURLResolver.reverse()
an inverse function of RegexURLResolver.resolve(). This has a number of
desirable properties; for example, generating a URL using the {{{ {{url}}
}}} template function will ''always'' generate a URL which will resolve to
the requested view function with the specified parameters.
If there are genuinely two different patterns which map to the exact same
view function and parameters, I will agree that there is no solution to
that except named patterns. However, I don't think that it should be
necessary to use named functions when there is only one reverse mapping
which preserves all the parameters.
--
Ticket URL: <http://code.djangoproject.com/ticket/4915#comment:2>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" 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-updates?hl=en
-~----------~----~----~----~------~----~------~--~---