146

In Django, how can I do a simple redirect directly from urls.py? Naturally I am a well organized guy, favoring the DRY principle, so I would like to get the target based on it's named url pattern, rather than hard coding the url.

thnee
  • 5,817
  • 3
  • 27
  • 23

6 Answers6

220

If you are on Django 1.4 or 1.5, you can do this:

from django.core.urlresolvers import reverse_lazy
from django.views.generic import RedirectView

urlpatterns = patterns('',
    url(r'^some-page/$', RedirectView.as_view(url=reverse_lazy('my_named_pattern'), permanent=False)),
    ...

If you are on Django 1.6 or above, you can do this:

from django.views.generic import RedirectView

urlpatterns = patterns('',
    url(r'^some-page/$', RedirectView.as_view(pattern_name='my_named_pattern', permanent=False)),
    ...

In Django 1.9, the default value of permanent has changed from True to False. Because of this, if you don't specify the permanent keyword argument, you may see this warning:

RemovedInDjango19Warning: Default value of 'RedirectView.permanent' will change from True to False in Django 1.9. Set an explicit value to silence this warning.

Flimm
  • 136,138
  • 45
  • 251
  • 267
thnee
  • 5,817
  • 3
  • 27
  • 23
  • 18
    Just a note, remember that the RedirectView has permanent=True as default. – Daniel Backman Jun 16 '14 at 11:32
  • 8
    You can redirect everything! Like so: ```(r'^.*/$', RedirectView.as_view(url='http://newurl.com')),``` – radtek May 08 '15 at 19:20
  • 3
    RedirectView will have permanent=False as default in Django 1.9. – Gravity Grave Jul 03 '15 at 23:29
  • 5
    Can someone explain what is 'my_named_pattern' in above example. – Jay Modi Dec 16 '15 at 10:51
  • 1
    passing `pattern_name` argument to `RedirectView` executes reverse for you at call time using given pattern name. Other [useful parameters include `permanent` and `query_string`](https://docs.djangoproject.com/en/1.10/ref/class-based-views/base/#django.views.generic.base.RedirectView.url). – tutuDajuju Sep 06 '16 at 16:29
  • If you don't pass `query_string=True`, then the redirect will lose the query string, eg: `?gclid=foobar`. – Flimm Jan 05 '17 at 11:12
  • In Django 2.1 and later, use `re_path()` not `url()`; see https://docs.djangoproject.com/en/2.1/ref/urls/#url – Nomen Nescio May 03 '19 at 17:23
41

This works for me.

from django.views.generic import RedirectView

urlpatterns = patterns('',
    url(r'^some-page/$', RedirectView.as_view(url='/')),
    ...

In above example '/' means it will redirect to index page, where you can add any url patterns also.

phoenix
  • 7,988
  • 6
  • 39
  • 45
Jay Modi
  • 3,161
  • 4
  • 35
  • 52
  • 5
    Just a note, remember that the `RedirectView` has `permanent=True` as default in older versions of Django, and `permanent=False` as default in Django versions >= 1.9. – Flimm Oct 03 '16 at 10:33
30

for django v2+

from django.contrib import admin
from django.shortcuts import redirect
from django.urls import path, include


urlpatterns = [
    # this example uses named URL 'hola-home' from app named hola
    # for more redirect's usage options: https://docs.djangoproject.com/en/2.1/topics/http/shortcuts/
    path('', lambda request: redirect('hola/', permanent=False)),
    path('hola/', include("hola.urls")),
    path('admin/', admin.site.urls),
]
shytikov
  • 9,155
  • 8
  • 56
  • 103
Glushiator
  • 630
  • 8
  • 8
  • 6
    @Ali Permanent redirects usually have HTTP code 301, temporary redirects usually have code 302. Permanent redirects are used to inform the browser of site's structural changes. Temporary redirects are used to indicate a new page to be shown after a dynamic server side action like login. According to the documentation `permanent=False` is the default. For more details please read: https://docs.djangoproject.com/en/2.1/topics/http/shortcuts/#redirect https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections#Permanent_redirections – Glushiator Mar 13 '20 at 14:39
11

I was trying to redirect all 404s to the home page and the following worked great:

from django.views.generic import RedirectView
# under urlpatterns, added:
url(r'^.*/$', RedirectView.as_view(url='/home/')),
url(r'^$', RedirectView.as_view(url='/home/')),
Jay Modi
  • 3,161
  • 4
  • 35
  • 52
Anurag
  • 121
  • 2
  • 8
9

This way is supported in older versions of django if you cannot support RedirectView

In view.py

def url_redirect(request):
    return HttpResponseRedirect("/new_url/")

In the url.py

url(r'^old_url/$', "website.views.url_redirect", name="url-redirect"),

You can make it permanent by using HttpResponsePermanentRedirect

Ajoy
  • 1,838
  • 3
  • 30
  • 57
sirFunkenstine
  • 8,135
  • 6
  • 40
  • 57
1

You could do straight on the urls.py just doing something like:

url(r'^story/(?P<pk>\d+)/',
        lambda request, pk: HttpResponsePermanentRedirect('/new_story/{pk}'.format(pk=pk)))

Just ensure that you have the new URL ready to receive the redirect!! Also, pay attention to the kind of redirect, in the example I'm using Permanent Redirect