Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate Admin/User authentication system in Django

I've recently started learning/using django; I'm trying to figure out a way to have two separate authentications systems for administrators and users. Rather than create a whole new auth system, I'd like to leverage django's built-in functionality (i.e. session management, @login_required decorator, etc.).

Specifically, I want to have two separate login tables - one for admins, one for users. The admin login table should be the default table that django generates with its default fields (ie. id, username, email, is_staff, etc.). The user table, on the other hand, I want to have only 5 fields - id, email, password, first_name, last_name. Furthermore, I want to use django built-in session management for both login tables and the @login_required decorator for their respective views. Lastly, I want two separate and distinct login forms for admins and users.

Anyone have any suggestions on how I can achieve my goal or know of any articles/examples that could help me along?

like image 489
Dan Avatar asked Sep 29 '09 19:09

Dan


1 Answers

If I understand your question correctly (and perhaps I don't), I think you're asking how to create a separate login form for non-admin users, while still using the standard Django authentication mechanisms, User model, etc. This is supported natively by Django through views in django.contrib.auth.views.

You want to start with django.contrib.auth.views.login. Add a line to your urlconf like so:

(r'^/login/$', 'django.contrib.auth.views.login', {'template_name': 'myapp/login.html'})

The login generic view accepts the template_name parameter, which is the path to your custom login template (there is a generic one you can use as well, provided by django.contrib.auth).

Full documentation on the login, logout, password_change, and other generic views are available in the Django Authentication Docs.

like image 134
dcrosta Avatar answered Sep 23 '22 02:09

dcrosta