Learn DJANGO with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
Django Simple Counter App
# views.py
from django.shortcuts import render
from django.http import HttpResponse
count = 0
def counter_view(request):
global count
if request.method == 'POST':
action = request.POST.get('action')
if action == 'increment':
count += 1
elif action == 'decrement':
count -= 1
elif action == 'reset':
count = 0
return render(request, 'counter.html', {'count': count})
# counter.html
"""
<!DOCTYPE html>
<html>
<head>
<title>Django Counter</title>
</head>
<body>
<h2>Counter: {{ count }}</h2>
<form method="post">
{% csrf_token %}
<button name="action" value="increment">+</button>
<button name="action" value="decrement">-</button>
<button name="action" value="reset">Reset</button>
</form>
</body>
</html>
"""
# urls.py
from django.urls import path
from .views import counter_view
urlpatterns = [
path('counter/', counter_view),
]
Demonstrates a simple Django app with a counter using views, URL routing, and templates.
2
Django Form Submission Example
# views.py
from django.shortcuts import render
def form_view(request):
submitted_data = None
if request.method == 'POST':
submitted_data = request.POST.get('data')
return render(request, 'form.html', {'data': submitted_data})
# form.html
"""
<form method='post'>
{% csrf_token %}
<input name='data' />
<button type='submit'>Submit</button>
</form>
{% if data %}
<p>You submitted: {{ data }}</p>
{% endif %}
"""
Handle a simple form submission and display submitted data.
3
Django Template Rendering Example
# views.py
from django.shortcuts import render
def hello_view(request):
context = {'name':'Alice'}
return render(request, 'hello.html', context)
# hello.html
"""
<h1>Hello, {{ name }}</h1>
"""
Render a template with context variables.
4
Django URL Parameters Example
# views.py
from django.shortcuts import render
def user_view(request, user_id):
return render(request, 'user.html', {'user_id': user_id})
# urls.py
from django.urls import path
from .views import user_view
urlpatterns = [
path('user/<int:user_id>/', user_view),
]
# user.html
"""
<p>User ID: {{ user_id }}</p>
"""
Capture URL parameters and display them.
5
Django Model Form Example
# models.py
from django.db import models
class Item(models.Model):
name = models.CharField(max_length=50)
# forms.py
from django.forms import ModelForm
from .models import Item
class ItemForm(ModelForm):
class Meta:
model = Item
fields = ['name']
# views.py
from django.shortcuts import render, redirect
from .forms import ItemForm
def add_item(request):
if request.method == 'POST':
form = ItemForm(request.POST)
if form.is_valid():
form.save()
return redirect('/')
else:
form = ItemForm()
return render(request, 'add_item.html', {'form': form})
# add_item.html
"""
<form method='post'>
{% csrf_token %}
{{ form.as_p }}
<button type='submit'>Add</button>
</form>
"""
Use Django ModelForm to create a new model instance.
6
Django List View Example
# views.py
from django.shortcuts import render
items = ['Apple','Banana','Cherry']
def list_view(request):
return render(request, 'list.html', {'items': items})
# list.html
"""
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
"""
Display a list of items using Django template.
7
Django Session Example
# views.py
from django.shortcuts import render
def session_view(request):
count = request.session.get('count', 0)
count += 1
request.session['count'] = count
return render(request, 'session.html', {'count': count})
# session.html
"""
<p>Visit count: {{ count }}</p>
"""
Use Django sessions to track user visits.
8
Django Redirect Example
# views.py
from django.shortcuts import redirect
def old_view(request):
return redirect('new_view')
def new_view(request):
return HttpResponse('This is the new view')
Redirect users from one view to another.
9
Django Template Inheritance Example
# base.html
"""
<html>
<head><title>{% block title %}Base{% endblock %}</title></head>
<body>
{% block content %}{% endblock %}
</body>
</html>
"""
# child.html
"""
{% extends 'base.html' %}
{% block title %}Child{% endblock %}
{% block content %}
<p>This is child content</p>
{% endblock %}
"""
Use base template and extend it in child template.
10
Django Static Files Example
# settings.py
STATIC_URL = '/static/'
# template.html
"""
{% load static %}
<link rel='stylesheet' href='{% static "style.css" %}'>
<script src='{% static "script.js" %}'></script>
"""
Serve static files like CSS or JS in Django templates.