This is the backend of a Learning Management System (LMS) built using Django and Django REST Framework. The backend provides a comprehensive REST API to manage users, courses, categories, lessons, quizzes, questions, choices, enrollments, progress, and notifications.
Clone the Repository
git clone https://github.com/hoangsonww/Fullstack-Learning-Management-System.git
cd Fullstack-Learning-Management-System/LMS-Backend
Create and Activate a Virtual Environment
python -m venv .venv
# On Windows
.venv\Scripts\activate
# On macOS/Linux
source .venv/bin/activate
Install Required Packages
pip install -r requirements.txt
Configure MongoDB and Redis
Make sure MongoDB and Redis are running on your machine or have the correct connection strings in settings.py
.
If you donβt have MongoDB and Redis installed, run the following commands to install them:
Apply Migrations
Make sure all migrations are applied:
python manage.py makemigrations
python manage.py migrate
Create a Superuser
To access the Django admin interface, you need to create a superuser:
python manage.py createsuperuser
Follow the prompts to set up the username, email, and password. Be sure to store or remember these credentials for later use. You will need them to log in to the admin interface and test the API endpoints
Seed Sample Data
Run the following command to seed the database with sample data:
python manage.py seed_sample_data
Start the Django development server:
python manage.py runserver
The server will start at http://127.0.0.1:8000/
.
To use the Django admin interface:
Here is how the admin interface looks like:
Endpoint | Method | Description |
---|---|---|
/api/users/ |
GET | Retrieve a list of all users. |
/api/users/{id}/ |
GET | Retrieve a specific user instance. |
/api/users/ |
POST | Create a new user instance. |
/api/users/{id}/ |
PUT | Update a specific user instance. |
/api/users/{id}/ |
DELETE | Delete a specific user instance. |
/api/courses/ |
GET | Retrieve a list of all courses. |
/api/courses/{id}/ |
GET | Retrieve a specific course instance. |
/api/courses/ |
POST | Create a new course instance. |
/api/courses/{id}/ |
PUT | Update a specific course instance. |
/api/courses/{id}/ |
DELETE | Delete a specific course instance. |
/api/categories/ |
GET | Retrieve a list of all categories. |
/api/categories/{id}/ |
GET | Retrieve a specific category instance. |
/api/categories/ |
POST | Create a new category instance. |
/api/categories/{id}/ |
PUT | Update a specific category instance. |
/api/categories/{id}/ |
DELETE | Delete a specific category instance. |
/api/lessons/ |
GET | Retrieve a list of all lessons. |
/api/lessons/{id}/ |
GET | Retrieve a specific lesson instance. |
/api/lessons/ |
POST | Create a new lesson instance. |
/api/lessons/{id}/ |
PUT | Update a specific lesson instance. |
/api/lessons/{id}/ |
DELETE | Delete a specific lesson instance. |
/api/quizzes/ |
GET | Retrieve a list of all quizzes. |
/api/quizzes/{id}/ |
GET | Retrieve a specific quiz instance. |
/api/quizzes/ |
POST | Create a new quiz instance. |
/api/quizzes/{id}/ |
PUT | Update a specific quiz instance. |
/api/quizzes/{id}/ |
DELETE | Delete a specific quiz instance. |
/api/questions/ |
GET | Retrieve a list of all questions. |
/api/questions/{id}/ |
GET | Retrieve a specific question instance. |
/api/questions/ |
POST | Create a new question instance. |
/api/questions/{id}/ |
PUT | Update a specific question instance. |
/api/questions/{id}/ |
DELETE | Delete a specific question instance. |
/api/choices/ |
GET | Retrieve a list of all choices. |
/api/choices/{id}/ |
GET | Retrieve a specific choice instance. |
/api/choices/ |
POST | Create a new choice instance. |
/api/choices/{id}/ |
PUT | Update a specific choice instance. |
/api/choices/{id}/ |
DELETE | Delete a specific choice instance. |
/api/enrollments/ |
GET | Retrieve a list of all enrollments. |
/api/enrollments/{id}/ |
GET | Retrieve a specific enrollment instance. |
/api/enrollments/ |
POST | Create a new enrollment instance. |
/api/enrollments/{id}/ |
PUT | Update a specific enrollment instance. |
/api/enrollments/{id}/ |
DELETE | Delete a specific enrollment instance. |
/api/progress/ |
GET | Retrieve a list of all progress records. |
/api/progress/{id}/ |
GET | Retrieve a specific progress record instance. |
/api/progress/ |
POST | Create a new progress record instance. |
/api/progress/{id}/ |
PUT | Update a specific progress record instance. |
/api/progress/{id}/ |
DELETE | Delete a specific progress record instance. |
/api/notifications/ |
GET | Retrieve a list of all notifications. |
/api/notifications/{id}/ |
GET | Retrieve a specific notification instance. |
/api/notifications/ |
POST | Create a new notification instance. |
/api/notifications/{id}/ |
PUT | Update a specific notification instance. |
/api/notifications/{id}/ |
DELETE | Delete a specific notification instance. |
Learning-Management-System/
βββ LMS-Backend
β βββ manage.py
β βββ requirements.txt
β βββ LICENSE
β βββ db.sqlite3
β βββ README.md
β βββ LMSBackend/
β β βββ settings.py
β β βββ urls.py
β β βββ asgi.py
β β βββ wsgi.py
β βββ core/
β β βββ management/
β β β βββ commands/
β β β βββ seed_sample_data.py
β β βββ migrations/
β β βββ models.py
β β βββ views.py
β β βββ tests.py
β β βββ admin.py
β β βββ apps.py
β β βββ serializers.py
β β βββ urls.py
β βββ ...
βββ LMS-Frontend
β βββ (Frontend code)
β βββ ...
βββ LICENSE
βββ .gitignore
βββ README.md
Important: Most API endpoints require authentication. You must first log in to obtain an authentication token.
Log in to Get a Token
Use the /api/auth/login/
endpoint to log in and get a token.
curl -X POST http://127.0.0.1:8000/api/auth/login/ -H "Content-Type: application/json" -d '{
"username": "your_username",
"password": "your_password"
}'
Replace your_username
and your_password
with your superuser credentials.
This will return a response with a token. You must include this token in the Authorization
header as Token <your_token_here>
for all subsequent API requests.
Use the Token in API Requests
Include the token in the Authorization
header:
-H "Authorization: Token <your_token_here>"
You can test the API using curl
, Postman, or Swagger UI.
curl
To test the API using curl
, use the commands below. Replace <your_token_here>
with the token obtained from the login endpoint.
List All Users
curl -X GET http://127.0.0.1:8000/api/users/ -H "Content-Type: application/json" -H "Authorization: Token <your_token_here>"
You should get something like this:
[
{
"id":"66dde39af395abfee65d1f86",
"username":"gibsonstacey",
"email":"xcaldwell@example.org",
"is_instructor":false,"is_student":false,
"bio":"Consider red specific within chance fund successful out.",
"profile_picture":"https://placekitten.com/970/531"
}
]
Create a New Course
curl -X POST http://127.0.0.1:8000/api/courses/ -H "Content-Type: application/json" -H "Authorization: Token <your_token_here>" -d '{
"title": "New Course",
"description": "This is a new course description.",
"instructor": "{user_id}",
"category": "{category_id}",
"price": 150.00,
"published": true
}'
Repeat similar curl
commands for other endpoints.
http://127.0.0.1:8000/api/users/
).Token <your_token_here>
) in the βAuthorizationβ header. Note that the token should be prefixed with Token
and a space.Here is how the Swagger UI looks like:
Here is how the Redoc UI looks like:
For example, to view the list of all lessons, go to http://127.0.0.1:8000/api/lessons/. The interface should look like this:
If you want to seed the database with realistic sample data, you can also run the seed_sample_data
management command:
python manage.py seed_sample_data
This command will populate the database with randomly generated users, courses, categories, lessons, quizzes, questions, choices, enrollments, progress records, and notifications.
Note: By default, the seed_sample_data
command will be executed when you run the python manage.py migrate
command. If you donβt want to seed the database at that time, you can disable it by setting SEED_SAMPLE_DATA_ON_MIGRATE = False
in the settings.py
file.
For example, you can use MongoDB Compass to view the data in your MongoDB database, RedisInsight to view the data in your Redis database, and Postman to test the API endpoints, like below:
This project is licensed under the MIT License. See the LICENSE file for more information.
In short, feel free to use, modify, and distribute this code for personal or commercial projects. I would appreciate it if you could provide attribution by linking back to this repository!
If you have any questions or need further assistance, feel free to contact me at hoangson091104@gmail.com.
With the setup complete, you can now fully explore and test the API endpoints of the Learning Management System backend using either curl
, Postman, or the Swagger UI. Donβt forget to authenticate first by logging in to obtain a token!