Skip to content

PrincewillDev/Simple-Rest-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Student Management API

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.

πŸš€ Features

  • 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

πŸ“‹ Student Model Fields

  • id: Auto-generated unique identifier
  • name: 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)

πŸ› οΈ Installation & Setup

Prerequisites

  • Python 3.8 or higher
  • pip (Python package installer)

Step 1: Clone or Download

# If you have git
git clone <repository-url>
cd student_management_api

# Or download and extract the ZIP file

Step 2: Create Virtual Environment (Recommended)

# Create virtual environment
python -m venv venv

# Activate virtual environment
# On Windows:
venv\Scripts\activate

# On macOS/Linux:
source venv/bin/activate

Step 3: Install Dependencies

pip install -r requirements.txt

Step 4: Database Setup

# Create database migrations
python manage.py makemigrations

# Apply migrations to create database tables
python manage.py migrate

Step 5: Create Admin User (Optional)

# Create a superuser for admin access
python manage.py createsuperuser

Follow the prompts to create your admin username, email, and password.

Step 6: Run the Server

python manage.py runserver

Your API will be available at: http://localhost:8000/

πŸ“š API Endpoints

Base URL: http://localhost:8000/api/students/

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

Query Parameters

  • ?is_active=true - Filter active students
  • ?is_active=false - Filter inactive students
  • ?grade=10th - Filter students by grade

πŸ§ͺ Testing the API

Using the Browsable API (Easiest for Beginners)

  1. Start the server: python manage.py runserver
  2. Visit: http://localhost:8000/api/students/
  3. Use the web interface to test all operations

Using curl (Command Line)

1. List all students

curl -X GET http://localhost:8000/api/students/

2. Create a new student

curl -X POST http://localhost:8000/api/students/ \
  -H "Content-Type: application/json" \
  -d '{
    "name": "John Doe",
    "age": 16,
    "grade": "10th",
    "is_active": true
  }'

3. Get a specific student

curl -X GET http://localhost:8000/api/students/1/

4. Update a student

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
  }'

5. Partially update a student

curl -X PATCH http://localhost:8000/api/students/1/ \
  -H "Content-Type: application/json" \
  -d '{
    "grade": "12th"
  }'

6. Delete a student

curl -X DELETE http://localhost:8000/api/students/1/

Using Postman

  1. Import the API endpoints into Postman
  2. Set the base URL to http://localhost:8000
  3. Test each endpoint with appropriate HTTP methods
  4. Use the JSON examples provided above for POST/PUT requests

πŸ–₯️ Admin Interface

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

πŸ“ Project Structure

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

🧩 Code Explanation

Models (students/models.py)

  • Defines the Student model with all required fields
  • Includes validation and string representation methods
  • Uses Django ORM for database interactions

Serializers (students/serializers.py)

  • Converts between Python objects and JSON
  • Handles data validation and transformation
  • Provides custom validation for age, name, and grade fields

Views (students/views.py)

  • Contains both class-based and function-based view examples
  • Handles HTTP requests and returns JSON responses
  • Implements all CRUD operations with proper error handling

URLs (students/urls.py)

  • Maps URL patterns to view functions
  • Follows RESTful conventions for clean URLs
  • Includes detailed comments explaining URL patterns

πŸ”§ Customization

Adding New Fields

  1. Add fields to the Student model in models.py
  2. Include new fields in the serializer
  3. Create and apply database migrations
  4. Update admin configuration if needed

Adding Validation

  • Add custom validation methods in serializers.py
  • Use Django model field validation in models.py
  • Implement business logic validation in views

Extending the API

  • Add new viewsets or views in views.py
  • Create corresponding URL patterns in urls.py
  • Consider creating new serializers for different use cases

πŸš€ Deployment

For Production

  1. Environment Variables: Use environment variables for sensitive settings
  2. Database: Consider PostgreSQL or MySQL for production
  3. Static Files: Configure static file serving
  4. WSGI Server: Use Gunicorn or similar WSGI server
  5. Reverse Proxy: Use Nginx for serving static files and load balancing

Quick Heroku Deployment

  1. Install Heroku CLI
  2. Create Procfile: web: gunicorn student_management.wsgi
  3. Add gunicorn to requirements.txt
  4. Deploy: heroku create your-app-name && git push heroku main

🀝 Contributing

This project is designed for learning purposes. Feel free to:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

πŸ“– Learning Resources

❓ Common Issues & Solutions

Issue: "No module named 'rest_framework'"

Solution: Install DRF with pip install djangorestframework

Issue: "Table doesn't exist"

Solution: Run python manage.py makemigrations then python manage.py migrate

Issue: "CORS errors when accessing from frontend"

Solution: Install and configure django-cors-headers

Issue: "Permission denied" errors

Solution: Check your REST_FRAMEWORK settings in settings.py

πŸ“ License

This project is created for educational purposes. Feel free to use and modify as needed for learning Django and DRF.

πŸ“ž Support

If you encounter any issues or have questions:

  1. Check the Django and DRF documentation
  2. Review the code comments for explanations
  3. Search for solutions on Stack Overflow
  4. 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!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages