Skip to content

A secure, scalable Django web application that implements role-based login authentication using MySQL as the database backend. This project supports user roles like Counsellor, HOD, Accountant, and Principal, and redirects each to a custom dashboard after login. Best for learners and can also implemented in another project.

Notifications You must be signed in to change notification settings

themrinal/django_role_based_login

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Role-Based Login System in Django with MySQL

A secure, scalable Django web application that implements role-based login authentication using MySQL as the database backend. This project supports user roles like Counsellor, HOD, Accountant, and Principal, and redirects each to a custom dashboard after login. Perfect for college or institutional admission workflows, internal panels, or any system requiring controlled role access.

Key Features

  • Role-based authentication using Django’s built-in User model
  • Extend user data with a custom UserProfile model
  • Secure login with role detection and dashboard redirection
  • Role & user management via the Django Admin Panel
  • MySQL integration for production-ready relational storage
  • Environment variables for configuration using python-decouple
  • Clean, modular app structure for easy scalability

Screenshots

image image image

Project Structure

image

Project Root (rolebasedlogin/)

File Purpose
.env Stores environment variables like DB_NAME, DB_USER, SECRET_KEY. Not tracked by Git (sensitive info).
.gitignore Tells Git which files/folders to ignore (e.g. __pycache__, .env, *.pyc).
manage.py Command-line utility to run/manage your Django project (e.g. runserver, migrate, createsuperuser).
README.md Markdown file for project documentation on GitHub.

rolebasedlogin/ (project folder)

File Purpose
__init__.py Marks the folder as a Python package.
settings.py Django project settings (installed apps, DB config, middleware, etc).
urls.py Root URL routing configuration. Includes URLs from the users app.
asgi.py / wsgi.py Entry points for serving the app with ASGI/WSGI servers in production. Leave untouched for now.

users/ (your app)

File Purpose
__init__.py Marks the users folder as a Python package.
admin.py Registers UserProfile in Django Admin to assign roles to users.
apps.py Django app config. Auto-generated. Rarely changed.
decorators.py Custom decorators like @role_required to restrict access by user role.
forms.py Contains LoginForm or any future custom forms.
models.py Defines UserProfile model for storing roles (counsellor, hod, etc.).
signals.py Automatically creates a UserProfile when a User is created (via Django signals).
tests.py Placeholder for unit tests. You can ignore for now unless writing tests.
urls.py Routes app-specific URLs like /user/login. Included in main urls.py.
views.py Handles requests/responses. Contains login logic and role-based redirection.

Getting Started

1. Clone the Repository

git clone https://github.com/yourusername/rolebasedlogin.git cd rolebasedlogin

2. Set Up Environment Variables

Create a .env file in the root directory with your database credentials:

DB_NAME=your_db_name
DB_USER=your_username
DB_PASSWORD=your_password
DB_HOST=localhost
DB_PORT=3306

3. Install Dependencies

pip install Django mysqlclient python-decouple

4. Run Migrations & Create Superuser

python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser

5. Start Development Server

python manage.py runserver

Visit: http://localhost:8000/user/

User Roles & Access

Each user must be assigned a role through the Django Admin Panel.

Role URL Path Access Page
Counsellor /user/counsellor_dashboard/ Student enquiry & basic form
HOD /user/hod_dashboard/ Department-level decisions
Accountant /user/accountant_dashboard/ Payment verification
Principal /user/principal_dashboard/ Final approval & admission print

How It Works

  • Admin creates a user in the Django Admin Panel.
  • A UserProfile is auto-generated via signals.
  • Admin assigns a role (e.g., counsellor, hod).
  • On login, user is authenticated and redirected to a role-specific dashboard.

Role-Based View Protection

Use the @role_required(['role1', 'role2']) decorator to restrict views:

@role_required(['hod'])
def hod_dashboard(request):

Contributing

Pull requests are welcome! If you have ideas for new features or want to improve role handling, fork the repo and submit a PR.

If this project helps you, please give it a ⭐ on GitHub to support it!

About

A secure, scalable Django web application that implements role-based login authentication using MySQL as the database backend. This project supports user roles like Counsellor, HOD, Accountant, and Principal, and redirects each to a custom dashboard after login. Best for learners and can also implemented in another project.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors