Introduction and Foundations

Django Quick Start Guide

This guide gets you from zero to a working Django application in under 30 minutes. Perfect for developers who want to see Django in action quickly or need a rapid refresher on Django fundamentals.

Django Quick Start Guide

This guide gets you from zero to a working Django application in under 30 minutes. Perfect for developers who want to see Django in action quickly or need a rapid refresher on Django fundamentals.

Prerequisites Checklist

Before starting, ensure you have:

  • Python 3.8+ installed (python --version)
  • pip package manager available (pip --version)
  • Text editor or IDE ready
  • Terminal/command prompt access
  • 30 minutes of focused time

5-Minute Setup

Step 1: Create Project Environment (2 minutes)

# Create project directory
mkdir django_quickstart
cd django_quickstart

# Create virtual environment
python -m venv venv

# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate

# Install Django
pip install django

# Verify installation
python -m django --version

Step 2: Create Django Project (1 minute)

# Create project
django-admin startproject quickstart .

# Verify structure
ls -la
# You should see: manage.py, quickstart/

Step 3: Initial Setup (2 minutes)

# Apply initial migrations
python manage.py migrate

# Create superuser (follow prompts)
python manage.py createsuperuser

# Start development server
python manage.py runserver

✅ Success Check: Visit http://127.0.0.1:8000/ - you should see Django's welcome page!

10-Minute Blog Application

Let's build a simple blog to demonstrate Django's core concepts.

Step 1: Create Blog App (1 minute)

# Stop server with Ctrl+C, then:
python manage.py startapp blog

# Register app in quickstart/settings.py

Add to INSTALLED_APPS in quickstart/settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',  # Add this line
]

Step 2: Create Blog Model (2 minutes)

Edit blog/models.py:

from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse

class Post(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        ordering = ['-created_at']
    
    def __str__(self):
        return self.title
    
    def get_absolute_url(self):
        return reverse('post_detail', kwargs={'pk': self.pk})

Step 3: Create and Apply Migrations (1 minute)

# Create migration
python manage.py makemigrations blog

# Apply migration
python manage.py migrate

Step 4: Register Model in Admin (1 minute)

Edit 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']
    list_filter = ['created_at', 'author']
    search_fields = ['title', 'content']

Step 5: Create Views (2 minutes)

Edit blog/views.py:

from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView
from .models import Post

class PostListView(ListView):
    model = Post
    template_name = 'blog/post_list.html'
    context_object_name = 'posts'
    paginate_by = 5

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

def home(request):
    recent_posts = Post.objects.all()[:3]
    return render(request, 'blog/home.html', {'posts': recent_posts})

Step 6: Create Templates (2 minutes)

Create template directories:

mkdir -p blog/templates/blog

Create blog/templates/blog/base.html:

<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}Quick Blog{% endblock %}</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 40px; }
        .header { background: #333; color: white; padding: 20px; margin: -40px -40px 40px -40px; }
        .post { border-bottom: 1px solid #eee; padding: 20px 0; }
        .meta { color: #666; font-size: 0.9em; }
        a { color: #007bff; text-decoration: none; }
        a:hover { text-decoration: underline; }
    </style>
</head>
<body>
    <div class="header">
        <h1><a href="{% url 'home' %}" style="color: white;">Quick Blog</a></h1>
        <nav>
            <a href="{% url 'home' %}" style="color: white;">Home</a> |
            <a href="{% url 'post_list' %}" style="color: white;">All Posts</a> |
            <a href="{% url 'admin:index' %}" style="color: white;">Admin</a>
        </nav>
    </div>
    {% block content %}{% endblock %}
</body>
</html>

Create blog/templates/blog/home.html:

{% extends 'blog/base.html' %}

{% block content %}
    <h2>Welcome to Quick Blog</h2>
    <p>A simple Django blog built in 10 minutes!</p>
    
    <h3>Recent Posts</h3>
    {% for post in posts %}
        <div class="post">
            <h4><a href="{% url 'post_detail' post.pk %}">{{ post.title }}</a></h4>
            <p class="meta">By {{ post.author }} on {{ post.created_at|date:"M d, Y" }}</p>
            <p>{{ post.content|truncatewords:30 }}</p>
        </div>
    {% empty %}
        <p>No posts yet. <a href="{% url 'admin:index' %}">Create one in the admin!</a></p>
    {% endfor %}
    
    <p><a href="{% url 'post_list' %}">View all posts →</a></p>
{% endblock %}

Step 7: Configure URLs (1 minute)

Create blog/urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('posts/', views.PostListView.as_view(), name='post_list'),
    path('post/<int:pk>/', views.post_detail, name='post_detail'),
]

Update quickstart/urls.py:

from django.contrib import admin
from django.urls import path, include

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

Test Your Application (5 minutes)

Step 1: Start Server and Add Content

# Start development server
python manage.py runserver

Step 2: Create Sample Content

  1. Visit http://127.0.0.1:8000/admin/
  2. Login with your superuser credentials
  3. Click "Posts" → "Add Post"
  4. Create 2-3 sample blog posts

Step 3: Test All Features

Visit these URLs to test your application:

Common Quick Fixes

Issue: Server Won't Start

# Check if port is in use
python manage.py runserver 8080

# Or kill existing process
lsof -ti:8000 | xargs kill -9  # macOS/Linux

Issue: Template Not Found

# Check template directory structure
ls -la blog/templates/blog/

# Ensure APP_DIRS is True in settings.py

Issue: Database Errors

# Reset database
rm db.sqlite3
python manage.py migrate
python manage.py createsuperuser

Next Steps Roadmap

Now that you have a working Django application:

Immediate (Next 30 minutes)

  • Add user authentication
  • Create a contact form
  • Add CSS framework (Bootstrap)
  • Implement user comments

Short-term (Next few hours)

  • Add image uploads
  • Implement user profiles
  • Create REST API endpoints
  • Add email notifications

Medium-term (Next few days)

  • Deploy to cloud platform
  • Add caching
  • Implement full-text search
  • Add automated testing

Quick Reference Commands

Development Commands

# Start new project
django-admin startproject myproject

# Create new app
python manage.py startapp myapp

# Database operations
python manage.py makemigrations
python manage.py migrate

# User management
python manage.py createsuperuser

# Development server
python manage.py runserver

# Django shell
python manage.py shell

Useful Django Shell Commands

# In Django shell (python manage.py shell)
from blog.models import Post
from django.contrib.auth.models import User

# Create objects
user = User.objects.get(username='admin')
post = Post.objects.create(
    title='My First Post',
    content='Hello, Django!',
    author=user
)

# Query objects
Post.objects.all()
Post.objects.filter(author=user)

Congratulations! You've built a complete Django blog application from scratch in under 30 minutes. This quick start guide demonstrates Django's power for rapid development while following best practices for maintainable, scalable applications.