A comprehensive Hospital Management System built with Django REST Framework and PostgreSQL. This system manages user authentication, patient records, doctor information, and appointments.
- User Authentication: JWT-based authentication with login and registration
- Patient Management: Create, read, update, and delete patient records
- Doctor Management: Manage doctor profiles and specializations
- Appointment System: Schedule and manage appointments between patients and doctors
- RESTful API: Well-structured API endpoints for all operations
- CORS Enabled: Frontend integration ready
Before you begin, ensure you have the following installed on your system:
- Python 3.8 or higher
- PostgreSQL 12 or higher
- pip (Python package manager)
- Git
- Backend Framework: Django 6.0.2
- API: Django REST Framework
- Authentication: JWT (djangorestframework-simplejwt)
- Database: PostgreSQL
- CORS: django-cors-headers
git clone https://github.com/Nithin98765/whatbytes_HMS_task.git
cd whatbytes_HMS_taskOn Windows:
python -m venv venv
venv\Scripts\activateOn macOS/Linux:
python3 -m venv venv
source venv/bin/activateCreate a requirements.txt file if not present:
Django==6.0.2
djangorestframework
djangorestframework-simplejwt
psycopg2-binary
django-cors-headersInstall the dependencies:
pip install -r requirements.txt-
Open PostgreSQL (pgAdmin or command line)
-
Create a new database:
CREATE DATABASE HMS;
-
Create a PostgreSQL user (if needed):
CREATE USER postgres WITH PASSWORD '246810@HMS'; GRANT ALL PRIVILEGES ON DATABASE HMS TO postgres;
-
Update database credentials in
HMS/settings.pyif different:DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'HMS', 'USER': 'postgres', 'PASSWORD': '246810@HMS', # Change this to your password 'HOST': 'localhost', 'PORT': '5432' } }
python manage.py makemigrations
python manage.py migratepython manage.py createsuperuserFollow the prompts to enter:
- Email address
- Name
- Password
python manage.py runserverThe server will start at: http://127.0.0.1:8000/
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/auth/register/ |
Register a new user |
| POST | /api/auth/login/ |
Login and get JWT tokens |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/patients/ |
List all patients |
| POST | /api/patients/ |
Create a new patient |
| GET | /api/patients/{id}/ |
Get patient details |
| PUT | /api/patients/{id}/ |
Update patient |
| DELETE | /api/patients/{id}/ |
Delete patient |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/doctors/ |
List all doctors |
| POST | /api/doctors/ |
Create a new doctor |
| GET | /api/doctors/{id}/ |
Get doctor details |
| PUT | /api/doctors/{id}/ |
Update doctor |
| DELETE | /api/doctors/{id}/ |
Delete doctor |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/appointment/ |
List all appointments |
| POST | /api/appointment/ |
Create a new appointment |
| GET | /api/appointment/{id}/ |
Get appointment details |
| PUT | /api/appointment/{id}/ |
Update appointment |
| DELETE | /api/appointment/{id}/ |
Delete appointment |
Request:
POST http://127.0.0.1:8000/api/auth/register/
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"password": "securepassword123"
}Response:
{
"message": "Users registered Successfully"
}Request:
POST http://127.0.0.1:8000/api/auth/login/
Content-Type: application/json
{
"email": "john@example.com",
"password": "securepassword123"
}Response:
{
"user": {
"id": 1,
"name": "John Doe",
"email": "john@example.com"
},
"refresh": "eyJ0eXAiOiJKV1QiLCJhbGc...",
"access": "eyJ0eXAiOiJKV1QiLCJhbGc..."
}For protected endpoints, include the access token in the Authorization header:
Authorization: Bearer <your_access_token># Register
curl -X POST http://127.0.0.1:8000/api/auth/register/ \
-H "Content-Type: application/json" \
-d '{"name":"Test User","email":"test@example.com","password":"test123"}'
# Login
curl -X POST http://127.0.0.1:8000/api/auth/login/ \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"test123"}'- Import the endpoints listed above
- For authentication endpoints, use POST method
- For protected endpoints, add Bearer token in Authorization tab
Access the Django REST Framework browsable API:
- Navigate to
http://127.0.0.1:8000/api/auth/register/ - You'll see a user-friendly interface to test endpoints
whatbytes_HMS_task/
β
βββ HMS/ # Main project settings
β βββ settings.py # Django settings
β βββ urls.py # Main URL routing
β βββ wsgi.py # WSGI config
β βββ asgi.py # ASGI config
β
βββ accounts/ # User authentication app
β βββ models.py # Custom User model
β βββ serializers.py # User serializers
β βββ views.py # Auth views (Register, Login)
β βββ urls.py # Auth URL patterns
β
βββ patients/ # Patient management app
β βββ models.py # Patient model
β βββ serializers.py # Patient serializers
β βββ views.py # Patient views
β βββ urls.py # Patient URL patterns
β
βββ doctors/ # Doctor management app
β βββ models.py # Doctor model
β βββ serializers.py # Doctor serializers
β βββ views.py # Doctor views
β βββ urls.py # Doctor URL patterns
β
βββ appointment/ # Appointment management app
β βββ models.py # Appointment model
β βββ serializers.py # Appointment serializers
β βββ views.py # Appointment views
β βββ urls.py # Appointment URL patterns
β
βββ manage.py # Django management script
βββ requirements.txt # Python dependencies
For production, it's recommended to use environment variables:
Create a .env file:
SECRET_KEY=your-secret-key-here
DEBUG=False
DATABASE_NAME=HMS
DATABASE_USER=postgres
DATABASE_PASSWORD=246810@HMS
DATABASE_HOST=localhost
DATABASE_PORT=5432
ALLOWED_HOSTS=localhost,127.0.0.1Install python-decouple:
pip install python-decoupleUpdate settings.py to use environment variables.
CORS is currently set to allow all origins (development only). For production, update in settings.py:
CORS_ALLOWED_ORIGINS = [
"http://localhost:3000",
"http://127.0.0.1:3000",
]1. PostgreSQL Connection Error
django.db.utils.OperationalError: could not connect to server
Solution:
- Ensure PostgreSQL is running
- Verify database credentials in
settings.py - Check if the database 'HMS' exists
2. Migration Errors
No changes detected
Solution:
python manage.py makemigrations accounts patients doctors appointment
python manage.py migrate3. CORS Errors in Browser
Access to fetch has been blocked by CORS policy
Solution:
- Ensure
corsheadersis inINSTALLED_APPS - Verify
CorsMiddlewareis first inMIDDLEWARE - Check
CORS_ALLOW_ALL_ORIGINS = Truein settings
4. Cache/304 Errors
304 Not Modified
Solution:
- Clear browser cache (Ctrl + Shift + Del)
- Use hard reload (Ctrl + Shift + R)
- Or use Incognito/Private mode
5. JWT Token Expired
Token is invalid or expired
Solution:
- Use the refresh token to get a new access token
- Re-login to get new tokens
- Access tokens expire in 15 minutes by default
Access the Django admin panel at: http://127.0.0.1:8000/admin/
Login with your superuser credentials to:
- Manage users
- View/edit patients
- View/edit doctors
- View/edit appointments
- Access admin-only features
# Create migrations
python manage.py makemigrations
# Apply migrations
python manage.py migrate
# Create superuser
python manage.py createsuperuser
# Open Django shell
python manage.py shell
# Run tests
python manage.py test
# Collect static files (production)
python manage.py collectstatic-
Activate virtual environment
venv\Scripts\activate # Windows source venv/bin/activate # macOS/Linux
-
Make your changes to models, views, serializers, etc.
-
Create and apply migrations if models changed
python manage.py makemigrations python manage.py migrate
-
Run the development server
python manage.py runserver
-
Test your endpoints using Postman, cURL, or the browsable API
- Change the
SECRET_KEYin production - Set
DEBUG = Falsein production - Use environment variables for sensitive data
- Implement rate limiting for APIs
- Use HTTPS in production
- Regularly update dependencies
- Never commit
.envfiles or sensitive credentials
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request