django redirect

django redirect  



1. HttpResponseDirect


2. redirect


3. 中直接redirect



down vote

It's simple:

from django.http import HttpResponseRedirect

def myview(request):
    return HttpResponseRedirect("/path/")

More info in the
official Django docs


There is apparently a better way of doing this in Django now using generic views.

Example –

from django.views.generic.simple import redirect_to

urlpatterns = patterns('',   
    (r'^one/$', redirect_to, {'url': '/another/'}),


There is more in the
generic views documentation.Credit –
Carles Barrobés.

+1: Quote the docs. – S.LottFeb
7 '09 at 17:22
This is no longer the best method as of Django 1.0. See this…
– JakeDec
16 '10 at 0:40

Depending on what you want (i.e. if you do not want to do any additional pre-processing), it is simpler to just use Django's redirect_to generic view:

from django.views.generic.simple import redirect_to

urlpatterns = patterns('',
    # static media (development only)    
    (r'^one/$', redirect_to, {'url': '/another/'}),


See for more advanced examples

+1 for using a generic view rather than implementing your own (no matter how simple) as in the (current) top voted answer. – DayDec
16 '10 at 0:33
+1 for simple, djangoey goodness. – musashiXXXApr
7 '11 at 19:53
Does anyone have any examples for if you do want to do additional pre-processing? – niallscoJun
4 '11 at 12:34
Then I'd suggest either write a custom view that does the processing and then calls the generic view, or write a decorator e.g. pre_process and decorate the generic view: (r'^one/$', pre_process(redirect_to), {'url': '/another/'})
– Carles BarrobésJun
6 '11 at 9:35
@niallsco: if you want to do additional processing, then it's best to use the redirect shortcut as described by Kennu inhere
– Lie RyanJul
21 '11 at 3:45

There's actually a simpler way than having a view for each redirect – you can do itdirectly in

from django.http import HttpResponsePermanentRedirect

urlpatterns = patterns(
    # ...normal patterns here...
     lambda request: HttpResponsePermanentRedirect('/nice-link')),

A target can be a callable as well as a string, which is what I'm using here.

True, but using the redirect_to generic view that comes with django is simpler still and more readable. See Carles…
– DayDec
16 '10 at 0:36

Since Django 1.1, you can also use the simpler
redirect shortcut:

from django.shortcuts import redirect

def myview(request):
    return redirect('/path')

It also takes an optional permanent=True keyword argument.


With Django version 1.3, the class based approach is:

from django.conf.urls.defaults import patterns, url
from django.views.generic import RedirectView

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

This example lives in in

You can do this in the Admin section. It's explained in the documentation.



版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则按侵权处理.


1 thought on “django redirect

Leave a Reply

Your email address will not be published. Required fields are marked *