Training Menu

README - Role Management with Django/Gihub: https://github.com/loic-houchi/MultipleuserDjango

TCHOULA HOUCHI LOIC RYAN
TCHOULA HOUCHI LOIC RYAN
Sept. 10, 2025 · 7.00 min read
0
Python
README - Role Management with Django/Gihub: https://github.com/loic-houchi/MultipleuserDjango

This document explains step by step how to build a system for managing user roles in Django (Doctor, Nurse, Patient).


Project & Application Setup

  • Project name: multipleuserproject

  • Application name: usersapp

1. Create a virtual environment

python -m venv venv
source venv/bin/activate   # Linux / Mac
venv\Scripts\activate      # Windows

2. Install Django

pip install Django

Configuration in settings.py

INSTALLED_APPS = [
    ...,
    'usersapp',
]

TEMPLATES = [
    {
        ...,
        'DIRS': ['templates'],
        ...
    }
]

HTML Templates

  • base.html → main structure with {% block content %}

  • doctor.html → page for doctors

  • infirmiere.html → page for nurses

  • patient.html → page for patients

  • login.html → login page with a form (username, password)

  • register.html → registration page with Django form

Registration Form (forms.py)

from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm

User = get_user_model()

class UserForm(UserCreationForm):
    ROLE_CHOICES = [
        ('doctor', 'Doctor'),
        ('infirmiere', 'Nurse'),
        ('patient', 'Patient'),
    ]
    role = forms.ChoiceField(choices=ROLE_CHOICES, required=True)

    class Meta:
        model = User
        fields = ['username', 'password1', 'password2', 'role']

    def save(self, commit=True):
        user = super().save(commit=False)
        role = self.cleaned_data['role']

        # Reset roles
        user.is_doctor = False
        user.is_infirmiere = False
        user.is_patient = False

        # Assign selected role
        if role == 'doctor':
            user.is_doctor = True
        elif role == 'infirmiere':
            user.is_infirmiere = True
        elif role == 'patient':
            user.is_patient = True

        if commit:
            user.save()
        return user

Custom User Model (models.py)

from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    is_doctor = models.BooleanField(default=False)
    is_infirmiere = models.BooleanField(default=False)
    is_patient = models.BooleanField(default=True)

Views (views.py)

from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from .form import UserForm

def home(request):
    error_message = None
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            if getattr(user, 'is_doctor', False):
                return redirect('doctor')
            elif getattr(user, 'is_infirmiere', False):
                return redirect('infirmiere')
            elif getattr(user, 'is_patient', False):
                return redirect('patient')
            else:
                error_message = "Role not defined."
        else:
            error_message = "Invalid username or password."
    return render(request, 'login.html', {'error_message': error_message})


def register(request):
    form = UserForm()
    if request.method == 'POST':
        form = UserForm(data=request.POST)
        if form.is_valid():
            form.save()
            return redirect('home')
    return render(request, 'register.html', {'form': form})


def doctor_view(request):
    return render(request, 'doctor.html')


def infirmiere_view(request):
    return render(request, 'infirmiere.html')


def patient_view(request):
    return render(request, 'patient.html')

URL Configuration

multipleuserproject/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('usersapp.urls')),
]

usersapp/urls.py

from django.urls import path
from usersapp.views import register, doctor_view, infirmiere_view, patient_view, home

urlpatterns = [
    path('', home, name='home'),
    path('register/', register, name='register'),
    path('doctor/', doctor_view, name='doctor'),
    path('infirmiere/', infirmiere_view, name='infirmiere'),
    path('patient/', patient_view, name='patient'),
]

Admin Configuration (admin.py)

from django.contrib import admin
from .models import User

admin.site.register(User)

With this setup, you now have a Django project with role-based authentication.
When a user logs in, they are redirected to their respective dashboard (doctor, nurse, or patient) depending on their assigned role

 

0

Applaudissez pour montrer votre soutien

TCHOULA HOUCHI LOIC RYAN

TCHOULA HOUCHI LOIC RYAN

0 Followers · Writer for Python