Redirect request to admin interface

Simon picture Simon · Jan 30, 2012 · Viewed 14.8k times · Source

After enabling the administrator interface and starting the development web server on e.g. 128.0.0.1:8000, I am able to reach the admin interface on

128.0.0.1:8000/admin.

Obviously due the following URL namespace

url(r'^admin/', include(admin.site.urls)).

What do I have to do redirect requests on 128.0.0.1:8000/ automatically to 128.0.0.1:8000/admin?

The URL namespace

url(r'^$/', include(admin.site.urls))

does not seem to be the solution.

Answer

Mariusz Jamro picture Mariusz Jamro · Feb 1, 2012

Use RedirectView. Instead of hardcoding URLs you can use reverse and the name of an admin view.

from django.core.urlresolvers import reverse
from django.views.generic.base import RedirectView

url(r'^$', RedirectView.as_view(url=reverse('admin:index')))