Request redirection to admin interface

After enabling the admin interface and starting the development web server, e.g. 128.0.0.1:8000, I can access the admin interface on

128.0.0.1:8000/admin. 

Obviously due to the following URL namespace

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

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

URL Namespace

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

doesn't seem to be a solution.

+8
source share
5 answers

Use RedirectView . Instead of hardcoded URLs, you can use reverse and the administrator name.

 from django.core.urlresolvers import reverse from django.views.generic.base import RedirectView url(r'^$', RedirectView.as_view(url=reverse('admin:index'))) 
+14
source

You say you want to redirect you to use django RedirectView

 from django.views.generic.base import RedirectView url(r'^$', RedirectView.as_view(url='/admin')) 
+7
source

This works for me. reverse_lazy did not.

Django 1.8.1 and higher

 urlpatterns = patterns('', url(r'^$', lambda x: HttpResponseRedirect('/admin')), ) 
+3
source

Previous solutions either map the redirect to a hard-coded URL or use methods that did not work here. This one works in Django 1.9 and is redirected to the admin index index:

 from django.shortcuts import redirect urlpatterns = patterns('', url(r'^$', lambda _: redirect('admin:index'), name='index'), ) 
+3
source

Django 2.0 and higher:

 from django.contrib import admin from django.urls import path, reverse_lazy from django.views.generic.base import RedirectView urlpatterns = [ path('', RedirectView.as_view(url=reverse_lazy('admin:index'))), path('admin/', admin.site.urls), ] 
0
source

All Articles