Description:
- Build an API to manage tasks where users can create, update, and delete tasks, and mark tasks as complete or incomplete. Why this project? Requirements:
- CRUD operations for tasks and users.
- Endpoint for marking tasks as complete or incomplete.
- Use Django ORM for database interactions.
- Deploy the API on Heroku or PythonAnywhere. Project Managment(TRELLO) KANBAN BOARD
Entity Relationship Diagram(ERD) ERD
Create a new directory for the project:
mkdir task_management_api
cd task_management_api
git init
git remote add origin [Repository Link](https://github.com/KhadijaMakkaoui/Task-Management-API.git)
pip install django
pip install djangorestframework
#Create an app for managing tasks
python manage.py startapp tasks
Add the app to INSTALLED_APPS in task_management/settings.py
#Create a virtual environment to isolate project dependencies
python -m venv venv
# Activate the virtual environment on Windows
venv\Scripts\activate
#Install required dependencies, including Django and Django REST Framework
pip install django djangorestframework
#Save dependencies to requirements.txt to simplify future setup
pip freeze > requirements.txt
#Run initial migrations for the project
python manage.py migrate
Install PostgreSQL and psycopg2 First, ensure that PostgreSQL is installed on your system. You will also need the psycopg2 package to allow Django to interact with the PostgreSQL database.
- Install psycopg2 using pip:
pip install psycopg2
- Modify the DATABASES setting in the settings.py file to use PostgreSQL instead of SQLite.
GET /tasks/ → List all tasks.
POST /tasks/ → Create a new task.
GET /tasks// → Retrieve a specific task.
PUT /tasks// or PATCH /tasks// → Update a specific task.
DELETE /tasks// → Delete a specific task.
User URLs (CRUD Operations):
GET /users/ → List all users.
POST /users/ → Create a new user.
GET /users// → Retrieve a specific user.
PUT /users// or PATCH /users// → Update a specific user.
DELETE /users// → Delete a specific user.
Custom Action URLs (Using @action):
PATCH /tasks//mark_complete/ → Mark a task as complete.
PATCH /tasks//mark_incomplete/ → Revert a task to incomplete.
- Install Required Packages:
pip install djangorestframework-simplejwt
- Configure JWT in Django:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
],
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated', # By default, require users to be logged in
],}
- Usage: You can now use the /api/token/ endpoint to obtain an access and refresh token by sending a POST request with valid user credentials (username and password).
Use the /api/token/refresh/ endpoint to refresh the access token using the refresh token.
- Implement task filtering and sorting in Django REST Framework
- install django filter
pip install django-filter
-
Update settings to include DjangoFilterBackend Add DEFAULT_FILTER_BACKENDS to the settings.py file
-
Define Filters and Ordering in the View
-
API Requests Filter by status: /api/tasks/?status=pending
Filter by priority: /api/tasks/?priority=high
Filter by due date: /api/tasks/?due_date=2024-10-07
Sort by due date: /api/tasks/?ordering=due_date
Sort by priority: /api/tasks/?ordering=priority_level
if you'd like to reverse the order, use a minus sign: /api/tasks/?ordering=-priority_level Filter by status and sort by due date: /api/tasks/?status=pending&ordering=due_date