[Django]-How to redirect (including URL change) in Django?

13👍

If you want to do it via code it is:

from django.http import HttpResponseRedirect

def frontpage(request):
    ...
    return HttpResponseRedirect('/index/')

But you can also do it directly in the urls rules:

from django.views.generic import RedirectView

urlpatterns = patterns('',
    (r'^$', RedirectView.as_view(url='/index/')),
)

For reference, see this post: https://stackoverflow.com/a/523366/5770129

3👍

What you could do, is the following. Put this in your urls.py:

url(r'^$',views.redirect_index),

and in your views:

def redirect_index(request):
    return redirect('index')

Leave a comment