Can we use the login page at /admin in django for our own use?
-
10-06-2021 - |
Question
Can I use the login page available at: /admin
for non-staff users to login? I'm using the following settings in my django settings file:
LOGIN_URL = '/admin/'
LOGIN_REDIRECT_URL = '/'
When I login, it doesn't redirect me to the root folder. Am I doing it the right way?
Note: I'm using decorator @login_required
on my view.
Edit
It logs me in to admin site with this URL: http://127.0.0.1:8000/admin/?next=/
Solution
Non-staff members can't login through the admin view, so you can't.
There is a Django view that does exactly what you need, however: django.contrib.auth.views.login
You can easily add it to your urlconf
:
from django.contrib.auth.views import login
urlpatterns = ('',
#snip
url(r'^login/$', login)
)
Check the documentation to see how you can customize its behavior: https://docs.djangoproject.com/en/dev/topics/auth/#limiting-access-to-logged-in-users
You'll only need to define a template for the view to use, by default, it should be located at registration/login.html
, but that can be overriden.
UPDATE
1) For django 1.11+ better use LoginView (i.e. from django.contrib.auth.views import LoginView
) since login
code actually uses LoginView and the code of login
even has a warning messge:
warnings.warn( 'The login() view is superseded by the class-based LoginView().', RemovedInDjango21Warning, stacklevel=2 )
2) You may want to change the default header of admin's login page. That can be done by providing site_header
in context.
So updated version would look like this:
from django.contrib.auth.views import LoginView
urlpatterns = [
# your patterns here,
url(r'^login/$',
LoginView.as_view(
template_name='admin/login.html',
extra_context={
'site_header': 'My custom header',
})),
]
OTHER TIPS
With Django 1.6 I was able to use django's own admin login template with the following setup. Then when I open '/' it will redirect me to the login screen, and after logging in it sends me back to '/'
settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core',
'south',
)
LOGIN_URL = '/login'
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.contrib.auth.views import login
admin.autodiscover()
urlpatterns = patterns(
'',
url(r'^', include('core.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^login/$', login, {'template_name': 'admin/login.html'})
# I didn't create this 'admin/login.html' template
# Django will use the one from the admin application ;-)
)
core/urls.py
from django.conf.urls import patterns, url
urlpatterns = patterns(
'core.views.web_views',
url(r'^$', 'home'),
)
core/views/web_views.py
from django.shortcuts import render_to_response
from django.template.context import RequestContext
__author__ = 'tony'
from django.contrib.auth.decorators import login_required
@login_required
def home(request):
return render_to_response('home.html', {}, context_instance = RequestContext(request))