Django Introduction
Understanding Django web framework fundamentals
🌐 What is Django?
Django is a free, open-source Python web framework that follows the Model-View-Template (MVT) pattern. It helps developers build complex, database-driven websites quickly with less code.
# Simple Django view example
from django.shortcuts import render
def home(request):
return render(request, 'home.html', {
'message': 'Welcome to Django!'
})
Output:
Welcome to Django!
Key Django Features
ORM (Object-Relational Mapping)
Work with databases using Python code
class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
Template Engine
Create dynamic HTML with Django templates
{% for item in items %}
<p>{{ item.name }}</p>
{% endfor %}
Authentication
Built-in user management system
from django.contrib.auth import login
login(request, user)
Admin Interface
Automatic admin panel for content management
admin.site.register(Article)
🔹 Django Architecture (MVT Pattern)
Django follows the Model-View-Template pattern, which separates data, logic, and presentation. This architecture makes your code organized, reusable, and easy to maintain for building scalable web applications.
# MODEL - Database structure
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=10, decimal_places=2)
# VIEW - Business logic
def product_list(request):
products = Product.objects.all()
return render(request, 'products.html', {'products': products})
# TEMPLATE - Presentation (products.html)
# {% for product in products %}
# <h3>{{ product.name }}</h3>
# <p>Price: ${{ product.price }}</p>
# {% endfor %}
Output:
Laptop
Price: $999.99
Mouse
Price: $29.99
🔹 Django vs Other Frameworks
Django stands out with its "batteries included" philosophy, providing everything you need out of the box:
Django Advantages:
- Full-Featured: Admin panel, ORM, authentication included
- Secure by Default: Protection against SQL injection, XSS, CSRF
- Scalable: Used by Instagram, Spotify, YouTube
- Great Documentation: Comprehensive guides and tutorials
- Large Community: Thousands of packages and active support
🔹 Django Components
Understanding the main building blocks of Django:
# 1. MODELS - Define your data
from django.db import models
class Blog(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=100)
published_date = models.DateTimeField(auto_now_add=True)
# 2. VIEWS - Handle requests
from django.shortcuts import render
def blog_detail(request, id):
blog = Blog.objects.get(id=id)
return render(request, 'blog.html', {'blog': blog})
# 3. URLS - Route requests
from django.urls import path
urlpatterns = [
path('blog/<int:id>/', blog_detail, name='blog_detail'),
]
# 4. TEMPLATES - Display data
# <h1>{{ blog.title }}</h1>
# <p>By {{ blog.author }}</p>
🔹 Who Uses Django?
Django powers some of the world's most popular websites:
Photo sharing platform with billions of users
Spotify
Music streaming service backend
Visual discovery and bookmarking
NASA
Space agency web applications
🔹 Django Philosophy
Django follows key design principles:
- Don't Repeat Yourself (DRY): Write code once, reuse everywhere
- Explicit is Better Than Implicit: Clear, readable code
- Loose Coupling: Components work independently
- Rapid Development: Build features quickly
# DRY Principle Example
# Instead of repeating code:
# user1 = User.objects.get(id=1)
# user2 = User.objects.get(id=2)
# Create a reusable function:
def get_user(user_id):
return User.objects.get(id=user_id)
user1 = get_user(1)
user2 = get_user(2)