This repository contains a Django REST Framework (DRF) starter project built by following the official DRF tutorial. The project is organized into six lessons, each placed in a separate branch, allowing you to follow the development process step by step. The project demonstrates the creation of an API for managing code snippets, with key features like authentication, serialization, and highlighting.
- Project Overview
- Features
- Lessons Overview
- Installation
- Usage
- Project Structure
- API Endpoints
- Contributing
- License
This project provides a basic API for creating, reading, updating, and deleting (CRUD) code snippets. It highlights Django REST Framework's core concepts, including:
- Serializers
- API Views and Viewsets
- URL routing
- Authentication and permissions
- Browsable API
The project is structured around the official Django REST Framework tutorial, providing a hands-on guide for learning API development with DRF.
- Snippet CRUD: Manage code snippets through the API.
- Syntax Highlighting: Supports language-based syntax highlighting for code snippets.
- Authentication: Basic authentication for managing access to the API.
- Browsable API: User-friendly interface for exploring the API through the browser.
- Pagination: Easily paginate through snippet records.
This project is broken into six lessons, each corresponding to a tutorial section from the official Django REST Framework tutorial. Each lesson is on a separate branch, so you can check out each branch as needed:
- Lesson 1 - Serialization: Introduction to serializers and the basics of converting models to JSON format. Branch:
tutorial-2
- Lesson 2 - Requests and Responses: Handling HTTP requests and responses in DRF. Branch:
tutorial-3
- Lesson 3 - Class-Based Views: Building reusable views using Django's class-based views. Branch:
tutorial-4
- Lesson 4 - Authentication and Permissions: Adding user authentication and permissions to the API. Branch:
tutorial-5
- Lesson 5 - Relationships and Hyperlinked APIs: Creating relationships between models and using hyperlinked APIs. Branch:
tutorial-6
- Lesson 6 - Viewsets and Routers: Simplifying view logic using viewsets and routers for automatic URL routing. Branch:
tutorial-7
Each branch contains the implementation specific to that lesson, helping you to understand the incremental development of the project.
Follow these steps to set up the project locally:
-
Clone the repository:
git clone https://github.com/Chaos-19/django-restframework-starter-project.git cd drf-starter-project
-
Checkout a specific lesson branch:
git checkout tutorial-1 # Replace with the lesson branch you want to explore
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install the required dependencies:
pip install -r requirements.txt
-
Apply migrations:
python manage.py migrate
-
Create a superuser for the admin interface:
python manage.py createsuperuser
-
Run the development server:
python manage.py runserver
The API will be available at
http://127.0.0.1:8000/
.
You can interact with the API via the browser using Django REST Framework's browsable API at:
- Snippets List:
http://127.0.0.1:8000/snippets/
- Snippet Details:
http://127.0.0.1:8000/snippets/<id>/
The API requires basic authentication for certain endpoints. You can log in with your superuser credentials.
drf-starter-project/
├── mysite/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
├── snippets/
│ ├── migrations/
│ ├── __init__.py
│ ├── admin.py
│ ├── models.py
│ ├── serializers.py
│ ├── views.py
│ ├── urls.py
├── manage.py
├── db.sqlite3
└── requirements.txt
- mysite/: The main project configuration directory.
- snippets/: Contains the core logic for the Snippet API (models, views, serializers).
- manage.py: A command-line utility for interacting with the project.
- requirements.txt: Lists the Python dependencies for the project.
- GET /snippets/: Retrieve a list of all snippets.
- POST /snippets/: Create a new snippet.
- GET /snippets/{id}/: Retrieve a specific snippet by ID.
- PUT /snippets/{id}/: Update a snippet by ID.
- DELETE /snippets/{id}/: Delete a snippet by ID.
- Basic Authentication is used to secure certain endpoints.
- Users can log in with the admin credentials to access protected routes.
Contributions are welcome! If you find any bugs or have suggestions, feel free to open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for more details.