📘

Django REST Framework Course by Bobby Stearman

Jul 1, 2024

Django REST Framework Course by Bobby Stearman

Introduction

  • Instructor: Bobby Stearman, senior software engineer
  • Platform: freeCodeCamp
  • Course Focus: Building web APIs using Django REST framework
  • Duration: ~1.5 hours
  • Modules: 8

What is Django REST framework?

  • A toolkit to build web APIs using Python
  • Allows handling of requests (GET, POST, PUT, DELETE)
  • Validates incoming information and responds to users
  • Can be used as a backend with frontend frameworks (React, Vue)

Course Outline

Module 1: Initial Setup

  • Prerequisites: Python 3.10, Docker, Docker Compose
  • Setup Steps:
    • Clone repository from GitHub
    • Create a directory drf_course
    • Install necessary extensions and tools (VS Code, Docker)
    • Structure: backend, steps, various configuration files
  • Commands:
    • Create virtual environment: python -m venv venv
    • Activate virtual environment: venv\Scripts\activate.bat (Windows)
    • Install dependencies: pip install -r backend/requirements.txt
    • Start Django project: django-admin startproject backend/drf_course
  • Files: settings.py, manage.py, .env template

Module 2: Setup Configurations

  • Add Core App: python manage.py startapp core in backend directory
  • Configure Settings: .env variables, installed apps, REST framework settings
  • Add URLs: Configure URL routing for core app
  • Database Migration: python manage.py makemigrations and python manage.py migrate
  • Run Server: python manage.py runserver
  • Check Endpoint: Check default Django page at localhost:8000

Module 3: Building Core App

  • Models: Define contact model
  • Serializers: Create contact serializer
  • Views: Handle API logic (POST requests for contact)
  • URLs: Map URLs to views
  • Testing: Use HTTPie to test API endpoint
  • Docker Integration: Use Docker to run and test the project

Module 4: Testing Core App

  • Write Tests: Create test cases for core functionality
  • Run Tests: Use python manage.py test to run tests
  • Database Validation: Verify data using Django shell

Module 5: Token Authentication

  • Add Token Auth: Update settings for token authentication
  • Setup Signals: Automatically create token for new users using signals
  • Create Superuser: Add admin user and check token creation
  • Test Endpoint: Get token using HTTPie

Module 6: E-commerce App Setup

  • Create Models: Define item and order models
  • Serializers: Create serializers for item and order
  • Migrate DB: python manage.py makemigrations and python manage.py migrate
  • Admin Interface: Add models to admin for easy testing

Module 7: E-commerce Views and Routers

  • Views: Define views for listing items and orders
  • Routers: Configure routers for e-commerce views
  • Permissions: Set up permissions for authenticated access

Module 8: Testing E-commerce App

  • Write Tests: Create extensive test cases for e-commerce endpoints
  • Run Tests: Ensure all functionality works as expected
  • Final Testing: Use HTTPie to make requests and validate responses

Conclusion

  • Final Configuration: Validate final directory structure
  • Key Takeaways: Fully functional API with authentication, dynamic endpoints, error handling
  • Acknowledgements: FreeCodeCamp for platform support and promotion

Additional Resources