Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#21177 closed Bug (fixed)

redirect() doesn't support redirecting to relative urls

Reported by: acatton@… Owned by: nobody
Component: Core (URLs) Version: master
Severity: Normal Keywords: redirect url relative
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no



If we use redirect shortcut to redirect to a relative URL, it fails.

Like in this example:

from django.conf.urls import patterns, url
from django.shortcuts import redirect

Using redirect to red

urlpatterns = patterns('',
    url(r'^foo/bar/$', lambda request: redirect('../baz/')),

It raises a TypeError:

Traceback (most recent call last):
  File "lib/python2.7/site-packages/django/core/handlers/", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "foo/foo/", line 9, in <lambda>
    url(r'^foo/bar/$', lambda request: redirect('../baz/')),
  File "lib/python2.7/site-packages/django/shortcuts/", line 78, in redirect
    return redirect_class(resolve_url(to, *args, **kwargs))
  File "lib/python2.7/site-packages/django/shortcuts/", line 151, in resolve_url
    return urlresolvers.reverse(to, args=args, kwargs=kwargs)
  File "lib/python2.7/site-packages/django/core/", line 496, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "lib/python2.7/site-packages/django/core/", line 379, in _reverse_with_prefix
    lookup_view = get_callable(lookup_view, True)
  File "lib/python2.7/site-packages/django/utils/", line 31, in wrapper
    result = func(*args)
  File "lib/python2.7/site-packages/django/core/", line 97, in get_callable
    mod = import_module(mod_name)
  File "lib/python2.7/site-packages/django/utils/", line 28, in import_module
    raise TypeError("relative imports require the 'package' argument")

However, it looks like there's support for relative URL:

Here's my proposed patch in this case:

Change History (4)

comment:1 Changed 5 years ago by Daniele Procida

Triage Stage: UnreviewedAccepted

comment:2 Changed 5 years ago by acatton@…

Is it possible to get this in master before Django 1.7?

comment:3 Changed 5 years ago by Tim Graham

Resolution: fixed
Status: newclosed

In e3d0790bd0b036ed3589659c1196e2c571e3dd8e:

Fixed #21177 -- Made resolve_url support relative URLs.

This fixes redirecting to relative URLs with django.shortcuts.redirect.

comment:4 Changed 5 years ago by Tim Graham <timograham@…>

In 8251438cb8c1d47779e9fc33cfa84dad9851a774:

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

Thanks apollo13: "reverse() shouldn't know about absolute/relative paths."

Note: See TracTickets for help on using tickets.
Back to Top