Wednesday, 22 October 2025

#17 Django

Django Masterclass: 10 Modules to Professional Web Apps

Django Masterclass: 10 Modules to Professional Web Apps

Step up to building robust, scalable web applications with Django! This 10-part blog series takes you from setup to production deployment through hands-on modules with code, explanations, and best practices.

Module 1: Introduction to Django & Installation

pip install django
django-admin startproject mysite
cd mysite
python manage.py runserver

Module 2: Django App Structure Explained

python manage.py startapp blog
  • mysite/ (project root)
  • blog/ (your app: models, views, templates)
  • manage.py (Django CLI)

Module 3: Models and Admin

# blog/models.py
from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()
    pub_date = models.DateTimeField(auto_now_add=True)
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser

Module 4: Views and URLs

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

def post_list(request):
    posts = Post.objects.all()
    return render(request, "post_list.html", {"posts": posts})
# blog/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path("", views.post_list, name="post_list"),
]
# mysite/urls.py
from django.contrib import admin
from django.urls import path, include

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

Module 5: Templates and Static Files

{% for post in posts %}
  <h2>{{ post.title }}</h2>
  <p>{{ post.body }}</p>
  <small>{{ post.pub_date }}</small>
{% endfor %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">
python manage.py collectstatic

Module 6: Forms and User Input

# blog/forms.py
from django import forms

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ["title", "body"]
<form method="POST">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Publish</button>
</form>

Module 7: Authentication (Login, Logout, Signup)

from django.contrib.auth import login, logout, authenticate
# Use Django's built-in auth views, templates, and User model for user management.
# mysite/urls.py (add)
from django.contrib.auth import views as auth_views
urlpatterns += [
    path("login/", auth_views.LoginView.as_view(), name="login"),
    path("logout/", auth_views.LogoutView.as_view(), name="logout"),
]
<a href="{% url 'login' %}">Log In</a>
<a href="{% url 'logout' %}">Log Out</a>

Module 8: Advanced Features – Class-based Views & Pagination

from django.views.generic import ListView
from .models import Post

class PostListView(ListView):
    model = Post
    paginate_by = 5
    template_name = "post_list.html"
# blog/urls.py (add)
path("posts/", PostListView.as_view(), name="post_list"),

Module 9: REST API with Django Rest Framework

pip install djangorestframework
# blog/api.py
from rest_framework import viewsets
from .models import Post
from .serializers import PostSerializer

class PostViewSet(viewsets.ModelViewSet):
    queryset = Post.objects.all()
    serializer_class = PostSerializer
# blog/serializers.py
from rest_framework import serializers
from .models import Post

class PostSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = "__all__"
# mysite/urls.py (add)
from rest_framework import routers
from blog.api import PostViewSet

router = routers.DefaultRouter()
router.register(r"posts", PostViewSet)
urlpatterns += [
    path("api/", include(router.urls)),
]

Module 10: Deployment to Production

  1. Create requirements.txt:
    pip freeze > requirements.txt
    
  2. Push to GitHub
    git init
    git add .
    git commit -m "Deploy Django app"
    git remote add origin https://github.com/yourname/yourrepo.git
    git push -u origin master
    
  3. Deploy on Render, Heroku, or any cloud provider:
    web: gunicorn mysite.wsgi
    
  4. Collect static files on server:
    python manage.py collectstatic
    
Django supports secure admin, scalable apps, and RESTful APIs with best practices and rapid development.

No comments:

Post a Comment

#20b Python (run+edit in browser ^ all in one)

๐Ÿ Python Basics — 5 Interactive Modules Edit code live with syntax highlighting and click ▶ Run . Each module runs separately ...