1. Home
  2. /
  3. Django
  4. /
  5. Simple Counter App

Simple Counter App - Django Typing CST Test

Loading…

Simple Counter App — Django Code

Demonstrates a simple Django app with a counter using views, URL routing, and templates.

# 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),
]

Django Language Guide

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.

Primary Use Cases

  • ▸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

Notable Features

  • ▸Object-relational mapping (ORM) for database operations
  • ▸Built-in authentication and user management
  • ▸Automatic admin interface
  • ▸Template system for HTML generation
  • ▸URL routing and middleware support

Origin & Creator

Django was created by Adrian Holovaty and Simon Willison in 2005 at the Lawrence Journal-World newspaper.

Industrial Note

Django is used heavily in industries requiring rapid prototyping, content management systems, and scalable web applications, e.g., news, e-commerce, and SaaS.

More Django Typing Exercises

Django Form Submission ExampleDjango Template Rendering ExampleDjango URL Parameters ExampleDjango Model Form ExampleDjango List View ExampleDjango Session ExampleDjango Redirect ExampleDjango Template Inheritance ExampleDjango Static Files Example

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher