Moved relative URL handling added in refs #21177 to a better place.

Thanks apollo13: "reverse() shouldn't know about absolute/relative paths."
This commit is contained in:
Tim Graham 2014-02-15 07:54:35 -05:00
parent be6d1f39e8
commit 8251438cb8
2 changed files with 6 additions and 4 deletions

View File

@ -383,10 +383,6 @@ class RegexURLResolver(LocaleRegexProvider):
text_args = [force_text(v) for v in args] text_args = [force_text(v) for v in args]
text_kwargs = dict((k, force_text(v)) for (k, v) in kwargs.items()) text_kwargs = dict((k, force_text(v)) for (k, v) in kwargs.items())
if isinstance(lookup_view, six.string_types):
# Handle relative URLs
if any(lookup_view.startswith(path) for path in ('./', '../')):
return lookup_view
try: try:
lookup_view = get_callable(lookup_view, True) lookup_view = get_callable(lookup_view, True)
except (ImportError, AttributeError) as e: except (ImportError, AttributeError) as e:

View File

@ -10,6 +10,7 @@ from django.db.models.base import ModelBase
from django.db.models.manager import Manager from django.db.models.manager import Manager
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from django.core import urlresolvers from django.core import urlresolvers
from django.utils import six
def render_to_response(*args, **kwargs): def render_to_response(*args, **kwargs):
@ -147,6 +148,11 @@ def resolve_url(to, *args, **kwargs):
if hasattr(to, 'get_absolute_url'): if hasattr(to, 'get_absolute_url'):
return to.get_absolute_url() return to.get_absolute_url()
if isinstance(to, six.string_types):
# Handle relative URLs
if any(to.startswith(path) for path in ('./', '../')):
return to
# Next try a reverse URL resolution. # Next try a reverse URL resolution.
try: try:
return urlresolvers.reverse(to, args=args, kwargs=kwargs) return urlresolvers.reverse(to, args=args, kwargs=kwargs)