Overview
This lecture introduces Django, covering setup, key concepts, project structure, debugging, and data modeling for an e-commerce backend. You'll learn how to start a project, create apps, define models, and best practices for organizing code.
Introduction to Django
- Django is a free, open-source web framework built with Python, popular for rapid web application development.
- It is "batteries included," providing features like admin interface, ORM (Object Relational Mapper), authentication, and caching out of the box.
- Django has a large, active community and is used by major companies (YouTube, Instagram, Spotify).
Prerequisites & Setup
- You need to know Python basics and object-oriented programming (classes, inheritance, polymorphism).
- Basic knowledge of relational databases (tables, columns, primary/foreign keys) is required.
- Use the latest Python version and install Visual Studio Code with the Python extension for development.
- Install Django in a virtual environment using pip or pipenv.
Project Structure & Apps
- A Django project is a collection of apps, each providing specific functionalities.
- Use
django-admin startproject to create a new project directory.
- Use
python manage.py startapp <app_name> to create new apps, e.g., playground.
- Register each new app in the
INSTALLED_APPS list in settings.py.
URLs, Views, and Templates
- Views are request handlers (functions) that return responses, not visual elements; templates handle HTML rendering.
- Map URLs to view functions in an app's
urls.py and include them in the main project's urls.py.
- Use Django's template system for dynamic HTML, passing context data from views.
Debugging Tools
- Integrated VS Code debugger allows step-by-step execution and variable inspection.
- Set up a
launch.json file for Django debugging in VS Code.
- Use Django Debug Toolbar for advanced debugging info, like SQL queries.
Web Development Fundamentals
- Web apps have front-end (client, visible to users) and back-end (server, processes data).
- HTTP (HyperText Transfer Protocol) handles client-server communication.
- Django is a back-end (server-side) framework, not to be confused with front-end frameworks like React.
API Focus in Django
- Django is commonly used to build APIs for client apps, exchanging data rather than HTML.
- Endpoints represent server functionality and are defined as part of the app's API.
Data Modeling in Django
- Identify core entities for an e-commerce app: Product, Collection (Category), Cart, Customer, Order, Tag.
- Define relationships: one-to-many (e.g. Collection to Product), many-to-many (e.g. Product to Cart via CartItem).
- Use association classes (e.g. CartItem, OrderItem) to model relationships with extra attributes like quantity.
App Organization Best Practices
- Avoid monolithic (all-in-one) apps; donβt split too granularly either.
- Group highly cohesive models (e.g., store features) together; separate reusable parts (e.g., tags) into distinct apps.
- Register all apps in
INSTALLED_APPS for Django to recognize them.
Key Terms & Definitions
- Django β Python web framework for building server-side applications.
- Virtual Environment β Isolated environment to manage dependencies.
- App β Self-contained module within a Django project for a specific feature.
- View β Function handling HTTP requests and returning responses.
- Template β HTML file for rendering dynamic content.
- Model β Class representing database tables/entities.
- URLconf β URL configuration mapping URLs to views.
- ORM (Object Relational Mapper) β Abstraction layer for database operations.
- API (Application Programming Interface) β Set of endpoints for client-server data exchange.
Action Items / Next Steps
- Upgrade Python to the latest version and install Visual Studio Code and Python extension.
- Install Django in a virtual environment and create your first project.
- Create and register at least one new app.
- Practice mapping URLs to views and rendering templates.
- Complete the exercise: Identify five core entities for an ecommerce app and their relationships.
- Prepare to implement model classes for the defined entities.