Learn Django - 10 Code Examples & CST Typing Practice Test
Django is a high-level Python web framework that encourages rapid development, clean design, and pragmatic code. It includes built-in tools for ORM, authentication, routing, and templating.
View all 10 Django code examples →
Learn DJANGO with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Frequently Asked Questions about Django
What is Django?
Django is a high-level Python web framework that encourages rapid development, clean design, and pragmatic code. It includes built-in tools for ORM, authentication, routing, and templating.
What are the primary use cases for Django?
Building dynamic web applications and websites. Developing RESTful APIs with Django REST Framework. Rapid prototyping of web projects. CMS and admin dashboard applications. E-commerce and SaaS applications
What are the strengths of Django?
Rapid development with built-in components. Secure defaults against common web vulnerabilities. Scalable for high-traffic websites. Large and active community with many third-party packages. Comprehensive documentation and tutorials
What are the limitations of Django?
Monolithic design can feel heavy for microservices. Learning curve for ORM and templating system. Slower than lightweight frameworks like Flask for small apps. Not as async-native as FastAPI. Some defaults may require customization for complex architectures
How can I practice Django typing speed?
CodeSpeedTest offers 10+ real Django code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.