Skip to content

Introduction to Django: Python's Powerful Web Framework

Published: at 10:00 AMSuggest Changes

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, Django takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel.

Table of Contents

Open Table of Contents

What is Django?

Django is a free and open-source web framework written in Python. It follows the Model-View-Template (MVT) architectural pattern and emphasizes the DRY (Don’t Repeat Yourself) principle. Django was originally developed to manage news-oriented sites for the Lawrence Journal-World newspaper, but has since evolved into one of the most popular web frameworks in the Python ecosystem.

Key Features of Django

1. Admin Interface

Django comes with a built-in admin interface that’s automatically generated based on your models. This feature allows you to manage your application’s data without writing additional code.

2. Object-Relational Mapping (ORM)

Django’s ORM allows you to interact with your database using Python code instead of SQL. It supports multiple database backends including PostgreSQL, MySQL, SQLite, and Oracle.

3. URL Routing

Django uses a clean, elegant URL design that makes your URLs both user-friendly and SEO-friendly.

4. Template Engine

Django’s template system separates design from Python code, allowing designers and developers to work independently.

5. Security Features

Django includes protection against many common security threats like SQL injection, cross-site scripting, cross-site request forgery, and clickjacking.

Installing Django

Before installing Django, make sure you have Python installed on your system. Django requires Python 3.8 or higher.

Prerequisites

First, ensure you have the latest version of pip:

# Update pip to the latest version
python -m pip install --upgrade pip

Step 1: Create a Virtual Environment

Virtual environments are essential for Django development as they isolate your project dependencies and prevent conflicts between different projects.

# Create a virtual environment (replace 'myproject' with your project name)
python -m venv myproject_env

Step 2: Activate the Virtual Environment

# On Windows
myproject_env\Scripts\activate

# On macOS/Linux
source myproject_env/bin/activate

You’ll know the virtual environment is active when you see (myproject_env) in your command prompt.

Step 3: Install Django

With your virtual environment activated, install Django:

pip install django

Step 4: Verify Installation

Confirm Django was installed correctly:

django-admin --version

This should display the Django version number (e.g., 5.0.1).

Why Use Virtual Environments?

Creating Your First Django Project

1. Start a New Project

django-admin startproject myproject
cd myproject

This creates a new Django project with the following structure:

myproject/
    manage.py
    myproject/
        __init__.py
        settings.py
        urls.py
        wsgi.py
        asgi.py

2. Run the Development Server

python manage.py runserver

Visit http://127.0.0.1:8000/ in your browser to see the Django welcome page.

Django Project Structure

Key Files Explained

Creating Your First Django App

Django projects are made up of multiple apps. An app is a web application that does something specific.

python manage.py startapp blog

This creates a new app with the following structure:

blog/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py

Register Your App

Add your new app to the INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',  # Add your app here
]

Django Models

Models define the structure of your database. Here’s a simple blog model:

# blog/models.py
from django.db import models
from django.contrib.auth.models import User

class Post(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(unique=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    published = models.BooleanField(default=False)

    class Meta:
        ordering = ['-created_at']

    def __str__(self):
        return self.title

Creating and Running Migrations

# Create migration files
python manage.py makemigrations

# Apply migrations to the database
python manage.py migrate

Django Views

Views handle the logic of your web application. Here are examples of both function-based and class-based views:

Function-Based View

# blog/views.py
from django.shortcuts import render, get_object_or_404
from .models import Post

def post_list(request):
    posts = Post.objects.filter(published=True)
    return render(request, 'blog/post_list.html', {'posts': posts})

def post_detail(request, slug):
    post = get_object_or_404(Post, slug=slug, published=True)
    return render(request, 'blog/post_detail.html', {'post': post})

Class-Based View

# blog/views.py
from django.views.generic import ListView, DetailView
from .models import Post

class PostListView(ListView):
    model = Post
    template_name = 'blog/post_list.html'
    context_object_name = 'posts'
    queryset = Post.objects.filter(published=True)
    paginate_by = 10

class PostDetailView(DetailView):
    model = Post
    template_name = 'blog/post_detail.html'
    context_object_name = 'post'

URL Configuration

App URLs

Create a urls.py file in your app directory:

# blog/urls.py
from django.urls import path
from . import views

app_name = 'blog'
urlpatterns = [
    path('', views.PostListView.as_view(), name='post_list'),
    path('<slug:slug>/', views.PostDetailView.as_view(), name='post_detail'),
]

Project URLs

Include your app URLs in the main project:

# myproject/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls')),
    path('', include('blog.urls')),  # Make blog the homepage
]

Django Templates

Templates define how your data is presented. Create a templates directory structure:

blog/
    templates/
        blog/
            base.html
            post_list.html
            post_detail.html

Base Template

<!-- blog/templates/blog/base.html -->
<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}My Blog{% endblock %}</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <header>
        <h1><a href="{% url 'blog:post_list' %}">My Blog</a></h1>
    </header>
    <main>
        {% block content %}
        {% endblock %}
    </main>
</body>
</html>

Post List Template

<!-- blog/templates/blog/post_list.html -->
{% extends 'blog/base.html' %}

{% block title %}Blog Posts{% endblock %}

{% block content %}
    <h2>Latest Posts</h2>
    {% for post in posts %}
        <article>
            <h3><a href="{% url 'blog:post_detail' post.slug %}">{{ post.title }}</a></h3>
            <p>By {{ post.author }} on {{ post.created_at|date:"F d, Y" }}</p>
            <p>{{ post.content|truncatewords:50 }}</p>
        </article>
    {% empty %}
        <p>No posts yet.</p>
    {% endfor %}
{% endblock %}

Django Admin

Django’s admin interface is one of its standout features. To use it:

1. Create a Superuser

python manage.py createsuperuser

2. Register Your Models

# blog/admin.py
from django.contrib import admin
from .models import Post

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'created_at', 'published')
    list_filter = ('published', 'created_at', 'author')
    search_fields = ('title', 'content')
    prepopulated_fields = {'slug': ('title',)}
    date_hierarchy = 'created_at'

3. Access the Admin

Visit http://127.0.0.1:8000/admin/ and log in with your superuser credentials.

Django Forms

Forms handle user input validation and rendering:

# blog/forms.py
from django import forms
from .models import Post

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['title', 'slug', 'content', 'published']
        widgets = {
            'content': forms.Textarea(attrs={'rows': 10}),
        }

    def clean_slug(self):
        slug = self.cleaned_data['slug']
        if Post.objects.filter(slug=slug).exclude(pk=self.instance.pk).exists():
            raise forms.ValidationError('A post with this slug already exists.')
        return slug

Best Practices

1. Use Virtual Environments

Always use virtual environments to manage your project dependencies.

2. Environment Variables

Use environment variables for sensitive settings like secret keys and database credentials.

3. Database Migrations

Always create and run migrations when you change your models.

4. Static Files

Configure static files properly for production deployment.

5. Security

Keep Django updated and follow security best practices, especially when deploying to production.

Common Django Commands

# Create a new project
django-admin startproject projectname

# Create a new app
python manage.py startapp appname

# Create migrations
python manage.py makemigrations

# Run migrations
python manage.py migrate

# Create superuser
python manage.py createsuperuser

# Run development server
python manage.py runserver

# Django shell
python manage.py shell

# Collect static files (for production)
python manage.py collectstatic

Conclusion

Django is a powerful, batteries-included web framework that can help you build robust web applications quickly. Its emphasis on rapid development, security, and scalability makes it an excellent choice for both beginners and experienced developers.

Key takeaways:

As you continue learning Django, explore topics like Django REST Framework for APIs, class-based views, middleware, caching, and deployment strategies. The Django documentation is comprehensive and well-written, making it an excellent resource for deepening your knowledge.

Start building your first Django project today, and you’ll quickly see why it’s one of the most popular web frameworks in the Python ecosystem!


Previous Post
CI/CD with GitHub Actions: Automated Testing and Deployment
Next Post
Introduction to Seaborn: Statistical Data Visualization in Python