Web Development Basics
Django Introduction
A full-stack, batteries-included web framework for Python following the MVT pattern.
Interview: Industry standard for enterprise Python development. Frequently questioned on MVT structure, ORM design, and security middleware.
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It follows the Model-View-Template (MVT) architecture pattern and provides a "batteries-included" ecosystem, supplying built-in ORM, admin panels, user auth, and form handling.
Django MVT Architecture
- Model: Maps Python objects directly to SQL database tables using Django ORM.
- View: Contains the business logic, handles request inputs, queries models, and determines what response/template to return.
- Template: The presentation layer. Django uses its own templating language to dynamically render HTML.
Class-Based Views vs Function Views
Views can be written as simple Python functions or as Class-Based Views (CBVs). CBVs allow common web patterns (like DetailView, ListView, CreateView) to be inherited and customized, reducing boilerplate code.
Interview Insight
Be ready to explain how Django protects apps by default. Django includes built-in middleware to protect against: CSRF (Cross-Site Request Forgery) via csrf tokens, SQL Injection via ORM parameterization, and XSS (Cross-Site Scripting) via auto-escaping in templates. Knowing how these security protections function is highly valued.
Use Cases
Enterprise Web Applications — Building large, scalable web services utilizing built-in security and administrative features.
Content Management Systems (CMS) — Leveraging Django built-in admin panel to manage content editors and permissions easily.
E-commerce Sites — Integrating database modeling, authentication, payment gateways, and security middleware under one framework.
Common Mistakes
Not understanding Django migrations — Editing model fields and forgetting to run `makemigrations` and `migrate`, leading to schema-mismatch runtime errors.
Heavy views, thin models — Putting too much business logic inside views instead of encapsulating it within helper methods in models (Fat models, skinny views is the Django best practice).
Exposing SECRET_KEY — Committing `settings.py` containing the raw SECRET_KEY to public Git repositories.