Python + Django page redirect

后端 未结 10 1747
别那么骄傲
别那么骄傲 2020-11-28 02:20

How do I accomplish a simple redirect (e.g. cflocation in ColdFusion, or header(location:http://) for PHP) in Django?

相关标签:
10条回答
  • 2020-11-28 02:23

    This should work in most versions of django, I am using it in 1.6.5:

    from django.core.urlresolvers import reverse
    from django.http import HttpResponseRedirect
    urlpatterns = patterns('',
        ....
        url(r'^(?P<location_id>\d+)/$', lambda x, location_id: HttpResponseRedirect(reverse('dailyreport_location', args=[location_id])), name='location_stats_redirect'),
        ....
    )
    

    You can still use the name of the url pattern instead of a hard coded url with this solution. The location_id parameter from the url is passed down to the lambda function.

    0 讨论(0)
  • 2020-11-28 02:25

    Beware. I did this on a development server and wanted to change it later.

    • Firefox 5 'caching' 301 redirects

    I had to clear my caches to change it. In order to avoid this head-scratching in the future, I was able to make it temporary like so:

    from django.views.generic import RedirectView
    
    url(r'^source$', RedirectView.as_view(permanent=False, 
                                          url='/dest/')),
    
    0 讨论(0)
  • 2020-11-28 02:32

    It's simple:

    from django.http import HttpResponseRedirect
    
    def myview(request):
        ...
        return HttpResponseRedirect("/path/")
    

    More info in the official Django docs

    Update: Django 1.0

    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.

    Update #2: Django 1.3+

    In Django 1.5 redirect_to no longer exists and has been replaced by RedirectView. Credit to Yonatan

    from django.views.generic import RedirectView
    
    urlpatterns = patterns('',
        (r'^one/$', RedirectView.as_view(url='/another/')),
    )
    
    0 讨论(0)
  • 2020-11-28 02:32

    There's actually a simpler way than having a view for each redirect - you can do it directly 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.

    0 讨论(0)
  • 2020-11-28 02:33

    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('',
        (r'^one/$', redirect_to, {'url': '/another/'}),
    
        #etc...
    )
    

    See documentation for more advanced examples.


    For Django 1.3+ use:

    from django.views.generic import RedirectView
    
    urlpatterns = patterns('',
        (r'^one/$', RedirectView.as_view(url='/another/')),
    )
    
    0 讨论(0)
  • 2020-11-28 02:34

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

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

    0 讨论(0)
提交回复
热议问题