Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use the login page at /admin in django for our own use?

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=/

like image 473
wasimbhalli Avatar asked May 23 '12 13:05

wasimbhalli


1 Answers

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',
            })),
]
like image 69
Thomas Orozco Avatar answered Oct 24 '22 16:10

Thomas Orozco