Skip to content

Sani-Yadav/DRF-Practice-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ DRF-Practice-API

Django REST Framework Practice Project - Learn RESTful APIs, CRUD operations, and Token Authentication with a complete working example.

πŸ“‹ Table of Contents

🎯 About

This is a comprehensive Django REST Framework practice project designed to help you learn:

  • RESTful API Development
  • CRUD Operations (Create, Read, Update, Delete)
  • Token Authentication
  • Serializers and Viewsets
  • Database Models and Migrations
  • Professional Project Structure

Perfect for beginners and intermediate developers who want to master Django REST Framework!

✨ Features

  • πŸ” Token Authentication - Secure API access
  • πŸ“ CRUD Operations - Full Create, Read, Update, Delete functionality
  • 🎯 Two Django Apps - webapp and webapp2 for different use cases
  • πŸ“Š SQLite Database - Easy to set up and use
  • πŸ”§ Professional Structure - Industry-standard project organization
  • πŸ“š Well Documented - Clear code comments and examples

πŸ›  Tech Stack

  • Backend Framework: Django 5.2.3
  • API Framework: Django REST Framework 3.16.0
  • Database: SQLite3
  • Authentication: Token Authentication
  • Language: Python 3.x

πŸ“ Project Structure

DRF-Practice-API/
β”œβ”€β”€ base/                          # Main Django Project
β”‚   β”œβ”€β”€ base/                      # Django Settings
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ settings.py            # Project settings
β”‚   β”‚   β”œβ”€β”€ urls.py               # Main URL configuration
β”‚   β”‚   β”œβ”€β”€ asgi.py
β”‚   β”‚   └── wsgi.py
β”‚   β”œβ”€β”€ webapp/                    # First Django App
β”‚   β”‚   β”œβ”€β”€ models.py             # Database models
β”‚   β”‚   β”œβ”€β”€ views.py              # API views
β”‚   β”‚   β”œβ”€β”€ serializers.py        # Data serialization
β”‚   β”‚   β”œβ”€β”€ urls.py               # App URL patterns
β”‚   β”‚   β”œβ”€β”€ admin.py              # Admin interface
β”‚   β”‚   └── migrations/           # Database migrations
β”‚   β”œβ”€β”€ webapp2/                   # Second Django App
β”‚   β”‚   β”œβ”€β”€ models.py             # Additional models
β”‚   β”‚   β”œβ”€β”€ views.py              # More API views
β”‚   β”‚   β”œβ”€β”€ serializers.py        # Additional serializers
β”‚   β”‚   β”œβ”€β”€ urls.py               # App URL patterns
β”‚   β”‚   └── migrations/           # Database migrations
β”‚   β”œβ”€β”€ manage.py                  # Django management script
β”‚   └── db.sqlite3                # SQLite database
β”œβ”€β”€ .gitignore                     # Git ignore rules
└── README.md                      # This file

πŸš€ Installation & Setup

Prerequisites

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

Step 1: Clone the Repository

git clone https://github.com/Sani-Yadav/DRF-Practice-API.git
cd DRF-Practice-API

Step 2: Create Virtual Environment

# Windows
python -m venv myenv
myenv\Scripts\activate

# macOS/Linux
python3 -m venv myenv
source myenv/bin/activate

Step 3: Install Dependencies

cd base
pip install django djangorestframework

Step 4: Run Migrations

python manage.py makemigrations
python manage.py migrate

Step 5: Create Superuser (Optional)

python manage.py createsuperuser

Step 6: Run the Server

python manage.py runserver

πŸŽ‰ Your API is now running at: http://127.0.0.1:8000/

πŸ“‘ API Endpoints

Authentication

  • POST /api/token/ - Get authentication token
  • POST /api/token/refresh/ - Refresh token

WebApp Endpoints

  • GET /api/webapp/ - List all items
  • POST /api/webapp/ - Create new item
  • GET /api/webapp/{id}/ - Get specific item
  • PUT /api/webapp/{id}/ - Update item
  • DELETE /api/webapp/{id}/ - Delete item

WebApp2 Endpoints

  • GET /api/webapp2/ - List all items
  • POST /api/webapp2/ - Create new item
  • GET /api/webapp2/{id}/ - Get specific item
  • PUT /api/webapp2/{id}/ - Update item
  • DELETE /api/webapp2/{id}/ - Delete item

πŸ’‘ Usage Examples

1. Get Authentication Token

curl -X POST http://127.0.0.1:8000/api/token/ \
  -H "Content-Type: application/json" \
  -d '{"username": "your_username", "password": "your_password"}'

2. Create New Item (with token)

curl -X POST http://127.0.0.1:8000/api/webapp/ \
  -H "Authorization: Token your_token_here" \
  -H "Content-Type: application/json" \
  -d '{"name": "Test Item", "description": "This is a test item"}'

3. Get All Items

curl -X GET http://127.0.0.1:8000/api/webapp/ \
  -H "Authorization: Token your_token_here"

4. Update Item

curl -X PUT http://127.0.0.1:8000/api/webapp/1/ \
  -H "Authorization: Token your_token_here" \
  -H "Content-Type: application/json" \
  -d '{"name": "Updated Item", "description": "Updated description"}'

πŸ” Authentication

This project uses Token Authentication for secure API access:

  1. Get Token: Send POST request to /api/token/ with username and password
  2. Use Token: Include token in Authorization header: Authorization: Token your_token_here
  3. Refresh Token: Use /api/token/refresh/ to get new token

Example Authentication Flow:

import requests

# Step 1: Get token
response = requests.post('http://127.0.0.1:8000/api/token/', {
    'username': 'your_username',
    'password': 'your_password'
})
token = response.json()['access']

# Step 2: Use token for API calls
headers = {'Authorization': f'Token {token}'}
response = requests.get('http://127.0.0.1:8000/api/webapp/', headers=headers)

πŸŽ“ Learning Path

For Beginners:

  1. Start with Models - Understand database structure in models.py
  2. Learn Serializers - See how data is formatted in serializers.py
  3. Study Views - Understand API logic in views.py
  4. Explore URLs - Learn routing in urls.py
  5. Test APIs - Use the examples above to test endpoints

For Intermediate Developers:

  1. Customize Models - Add new fields and relationships
  2. Extend Serializers - Add validation and custom methods
  3. Create New Views - Implement custom business logic
  4. Add Authentication - Implement custom authentication
  5. Optimize Performance - Add caching and database optimization

🀝 Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Commit your changes: git commit -m 'Add amazing feature'
  4. Push to the branch: git push origin feature/amazing-feature
  5. Open a Pull Request

πŸ“ License

This project is open source and available under the MIT License.

πŸ™ Acknowledgments

  • Django REST Framework team for the amazing framework
  • Django community for excellent documentation
  • All contributors who help improve this project

πŸ“ž Support

If you have any questions or need help:


Happy Coding! πŸš€

Made with ❀️ for the Django community

About

Practice project using Django REST Framework (DRF) to learn RESTful APIs, CRUD operations, and Token Authentication.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages