20👍
Django 2.0 and above:
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),
]
17👍
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')))
9👍
You say you want a redirect so you would use django’s RedirectView
from django.views.generic.base import RedirectView
url(r'^$', RedirectView.as_view(url='/admin'))
- Django – Difference between Database backed sessions and Cookie Based Session?
- Django rest framework nested viewsets and routes
5👍
You can also use path instead of url which has been deprecated and is no longer available in Django 4.
from django.views.generic.base import RedirectView
urlpatterns = [
path('', RedirectView.as_view(url='/admin')),
path('admin/', admin.site.urls),
...
]
- Django Pandas to http response (download file)
- What does it mean for an object to be unscriptable?
- How to find out whether a model's column is a foreign key?
4👍
This works for me. The reverse_lazy
did not.
Django 1.8.1 and above
urlpatterns = patterns('',
url(r'^$', lambda x: HttpResponseRedirect('/admin')),
)
- How to use bower package manager in Django App?
- How to filter objects by ignoring upper and lower case letter django
- Pycharm Can't retrieve image ID from build stream
4👍
Previous solutions either show redirection to a hard-coded URL or use methods that didn’t work here. This one works in Django 1.9 and redirects to the admin index view:
from django.shortcuts import redirect
urlpatterns = patterns('',
url(r'^$', lambda _: redirect('admin:index'), name='index'),
)
👤dev
- Passing a user, request to forms
- Django widget override template
- Where do I set environment variables for Django?
- Pycharm Django Debugging is really slow
- Django creating a form field that's read only using widgets
Source:stackexchange.com