A simple and beginner-friendly REST API built with Django and Django REST Framework for managing student records. This project is perfect for learning API development, CRUD operations, and Django REST Framework basics.
- Complete CRUD Operations: Create, Read, Update, and Delete students
- RESTful API Design: Follows REST conventions for clean and predictable endpoints
- Data Validation: Comprehensive validation for all student data
- Admin Interface: Built-in Django admin for easy data management
- API Documentation: Browsable API interface for easy testing
- Filtering Support: Filter students by active status and grade
- Beginner Friendly: Extensively commented code with clear explanations
id
: Auto-generated unique identifiername
: Student's full name (required)age
: Student's age in years (required, 5-100)grade
: Student's grade/class level (required)is_active
: Whether the student is currently active (default: True)created_at
: When the record was created (auto-generated)updated_at
: When the record was last updated (auto-generated)
- Python 3.8 or higher
- pip (Python package installer)
# If you have git
git clone <repository-url>
cd student_management_api
# Or download and extract the ZIP file
# Create virtual environment
python -m venv venv
# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate
pip install -r requirements.txt
# Create database migrations
python manage.py makemigrations
# Apply migrations to create database tables
python manage.py migrate
# Create a superuser for admin access
python manage.py createsuperuser
Follow the prompts to create your admin username, email, and password.
python manage.py runserver
Your API will be available at: http://localhost:8000/
Method | Endpoint | Description |
---|---|---|
GET | /api/students/ |
List all students |
POST | /api/students/ |
Create a new student |
GET | /api/students/{id}/ |
Get a specific student |
PUT | /api/students/{id}/ |
Update a student (full update) |
PATCH | /api/students/{id}/ |
Update a student (partial update) |
DELETE | /api/students/{id}/ |
Delete a student |
?is_active=true
- Filter active students?is_active=false
- Filter inactive students?grade=10th
- Filter students by grade
- Start the server:
python manage.py runserver
- Visit:
http://localhost:8000/api/students/
- Use the web interface to test all operations
curl -X GET http://localhost:8000/api/students/
curl -X POST http://localhost:8000/api/students/ \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"age": 16,
"grade": "10th",
"is_active": true
}'
curl -X GET http://localhost:8000/api/students/1/
curl -X PUT http://localhost:8000/api/students/1/ \
-H "Content-Type: application/json" \
-d '{
"name": "John Smith",
"age": 17,
"grade": "11th",
"is_active": true
}'
curl -X PATCH http://localhost:8000/api/students/1/ \
-H "Content-Type: application/json" \
-d '{
"grade": "12th"
}'
curl -X DELETE http://localhost:8000/api/students/1/
- Import the API endpoints into Postman
- Set the base URL to
http://localhost:8000
- Test each endpoint with appropriate HTTP methods
- Use the JSON examples provided above for POST/PUT requests
Access the Django admin interface at: http://localhost:8000/admin/
Use your superuser credentials to:
- View all students in a table format
- Add, edit, or delete students through a web interface
- Filter students by various fields
- Perform bulk actions on multiple students
student_management_api/
βββ manage.py # Django management script
βββ requirements.txt # Project dependencies
βββ README.md # This file
βββ student_management/ # Main project directory
β βββ __init__.py
β βββ settings.py # Django settings
β βββ urls.py # Main URL configuration
β βββ wsgi.py # WSGI configuration for deployment
βββ students/ # Students app
βββ __init__.py
βββ admin.py # Django admin configuration
βββ apps.py # App configuration
βββ models.py # Student model definition
βββ serializers.py # DRF serializers
βββ urls.py # App-specific URLs
βββ views.py # API views/endpoints
- Defines the Student model with all required fields
- Includes validation and string representation methods
- Uses Django ORM for database interactions
- Converts between Python objects and JSON
- Handles data validation and transformation
- Provides custom validation for age, name, and grade fields
- Contains both class-based and function-based view examples
- Handles HTTP requests and returns JSON responses
- Implements all CRUD operations with proper error handling
- Maps URL patterns to view functions
- Follows RESTful conventions for clean URLs
- Includes detailed comments explaining URL patterns
- Add fields to the Student model in
models.py
- Include new fields in the serializer
- Create and apply database migrations
- Update admin configuration if needed
- Add custom validation methods in
serializers.py
- Use Django model field validation in
models.py
- Implement business logic validation in views
- Add new viewsets or views in
views.py
- Create corresponding URL patterns in
urls.py
- Consider creating new serializers for different use cases
- Environment Variables: Use environment variables for sensitive settings
- Database: Consider PostgreSQL or MySQL for production
- Static Files: Configure static file serving
- WSGI Server: Use Gunicorn or similar WSGI server
- Reverse Proxy: Use Nginx for serving static files and load balancing
- Install Heroku CLI
- Create
Procfile
:web: gunicorn student_management.wsgi
- Add
gunicorn
to requirements.txt - Deploy:
heroku create your-app-name && git push heroku main
This project is designed for learning purposes. Feel free to:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
Solution: Install DRF with pip install djangorestframework
Solution: Run python manage.py makemigrations
then python manage.py migrate
Solution: Install and configure django-cors-headers
Solution: Check your REST_FRAMEWORK settings in settings.py
This project is created for educational purposes. Feel free to use and modify as needed for learning Django and DRF.
If you encounter any issues or have questions:
- Check the Django and DRF documentation
- Review the code comments for explanations
- Search for solutions on Stack Overflow
- Create an issue in the repository
Happy Learning! π
This Student Management API is designed to be your stepping stone into the world of Django REST Framework development. Take your time to understand each component, experiment with the code, and don't hesitate to make it your own!