Things have changed a bit in Django 1.5 with respect to how redirects work without writing a view. In Django 1.5, you must use “RedirectView”, but what may not be too obvious is that “reverse” does not work with “RedirectView”. You must to use “reverse_lazy” instead. I couldn’t find a complete example of this anywhere and hence it prompted me to write this post and answer this question on Stack Overflow. Hope this is helpful to someone.
Example url.py using RedirectView and reverse_lazy:
#!/usr/bin/env python # -*- coding: utf-8 -*- # vim: ai ts=4 sts=4 et sw=4 # Copyright 2013 Videntity # Freely reuse under the terms of http://www.apache.org/licenses/LICENSE-2.0.html # Last Updated: Aug 7 , 2013 from django.conf.urls import patterns, include, url from django.views.generic import RedirectView from django.core.urlresolvers import reverse_lazy # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() urlpatterns = patterns('apps.website.views', url(r'^$', 'home', name='website_home'), url(r'^redirect-home/$', RedirectView.as_view(url=reverse_lazy('website_home')), name='redirect_home'), )
So in the above example, the url “/redirect-home” will redirect to “/”. Hope this helps.