django redirect

django redirect  

 

以下文字摘自一篇讨论,其中提到了redirect的几种方法:

1. HttpResponseDirect

 

2. redirect

 

3. url.py 中直接redirect

 

 

80
down vote
accepted

It's simple:

from django.http import HttpResponseRedirect

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

More info in the
official Django docs

Update

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/'}),

    #etc...
)

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

 
1  
+1: Quote the docs. – S.LottFeb
7 '09 at 17:22
7  
This is no longer the best method as of Django 1.0. See this answer:stackoverflow.com/questions/523356/python-django-page-redirect/…
– JakeDec
16 '10 at 0:40
feedback

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/'}),

    #etc...
)

See
http://docs.djangoproject.com/en/dev/ref/generic-views/#django-views-generic-simple-redirect-to 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
feedback

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

from django.http import HttpResponsePermanentRedirect

urlpatterns = patterns(
    '',
    # ...normal patterns here...
    (r'^bad-old-link.php',
     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 answerstackoverflow.com/questions/523356/python-django-page-redirect/…
– DayDec
16 '10 at 0:36
feedback

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.

 
feedback

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 urls.py

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

https://docs.djangoproject.com/en/dev/ref/contrib/redirects/

link|edit

 

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

    分享到:

One Reply to “django redirect”

留言给Teodoro 取消

你的邮箱是保密的 必填的信息用*表示