Django provides many built-in features that make Python web development easier, faster, and more organized. Instead of creating common web application functionality from scratch, developers can use Django's built-in tools and reusable components.
Django is a high-level web framework built with Python. It provides a structured environment for developing web applications.
Developers can use Python for application logic while Django handles many common web development tasks.
Python
↓
Django
↓
Web Application
Django is designed to help developers build web applications efficiently.
It provides ready-to-use features such as URL routing, templates, forms, authentication, database models, and an administration interface.
Django follows an architecture commonly described as Model-Template-View (MTV).
Model
↓
Database
View
↓
Application Logic
Template
↓
Presentation
Django includes an Object-Relational Mapper, commonly called the ORM.
The ORM allows developers to interact with database records using Python models and QuerySets.
students = Student.objects.all()
for student in students:
print(student.name)
Django provides database backends that allow applications to work with supported relational database systems.
Commonly used database systems with Django include:
Database configuration is normally defined in the
DATABASES setting.
Django provides a URL dispatcher that maps requested URLs to views.
from django.urls import path
from . import views
urlpatterns = [
path(
"about/",
views.about,
name="about"
),
]
This makes it possible to organize the URLs of an application clearly.
Django includes a template system for generating HTML pages with dynamic data.
<h1>
Hello {{ name }}
</h1>
Templates can contain variables, filters, tags, loops, conditions, and template inheritance.
Django templates support inheritance. This allows developers to create a common base template and reuse it across multiple pages.
<!-- base.html -->
<html>
<body>
{% block content %}
{% endblock %}
</body>
</html>
Other templates can extend the base template instead of repeating the same HTML structure.
Django includes a built-in admin interface that can be used by authorized users to manage application data.
A model can be registered with the admin site.
from django.contrib import admin
from .models import Student
admin.site.register(Student)
Django includes an authentication framework for managing users, passwords, groups, permissions, and sessions.
Authentication can be used for:
Django's authentication system also includes authorization features that can control what authenticated users are allowed to do.
Permissions can be assigned to users or groups.
if request.user.has_perm(
"students.change_student"
):
# perform protected action
Django provides a form framework for creating and processing HTML forms.
from django import forms
class ContactForm(forms.Form):
name = forms.CharField(
max_length=100
)
email = forms.EmailField()
message = forms.CharField(
widget=forms.Textarea
)
Django forms provide validation functionality for checking submitted data.
For example, an EmailField can validate whether submitted data has a valid email format.
class ContactForm(forms.Form):
email = forms.EmailField()
Validation helps applications handle user input in a structured way.
Security is an important part of Django. The framework provides features that help developers address several common web security risks.
Django includes CSRF protection for requests that can change data. This helps protect applications against certain cross-site request forgery attacks.
Django templates commonly use the CSRF token inside POST forms.
<form method="post">
{% csrf_token %}
<button type="submit">
Submit
</button>
</form>
Middleware provides hooks into Django's request and response processing.
Middleware can be used for features such as:
Middleware is configured in the MIDDLEWARE setting.
Django provides support for static files such as CSS, JavaScript, and images.
STATIC_URL = "static/"
A template can use the static template tag to reference static assets.
{% load static %}
<script
src="{% static 'js/app.js' %}">
</script>
Django supports handling uploaded files through forms and request objects.
A model can use a FileField for storing information about an uploaded file.
class Document(models.Model):
title = models.CharField(
max_length=100
)
file = models.FileField(
upload_to="documents/"
)
Django can handle image uploads using an ImageField.
class Profile(models.Model):
name = models.CharField(
max_length=100
)
photo = models.ImageField(
upload_to="profiles/"
)
Image handling may require the Pillow package in the Python environment.
Django provides a session framework that allows applications to store data associated with a visitor across requests.
request.session["username"] = "Rahul"
Session data is useful for features such as login state, shopping carts, and temporary user information.
Django provides a messages framework for displaying temporary notifications to users.
from django.contrib import messages
messages.success(
request,
"Student added successfully."
)
Messages can be useful for success, warning, error, and informational notifications.
Django provides generic class-based views for common web development tasks.
Examples include views for displaying lists, individual objects, creating objects, updating objects, and deleting objects.
from django.views.generic import ListView
class StudentListView(ListView):
model = Student
Django supports class-based views in addition to function-based views.
from django.views import View
class HomeView(View):
def get(self, request):
return render(
request,
"home.html"
)
Class-based views can provide reusable structures for common request-handling patterns.
Django includes a caching framework that can reduce repeated processing and improve performance when used appropriately.
Django supports different cache backends, including local-memory caching and external cache systems.
CACHES = {
"default": {
"BACKEND":
"django.core.cache.backends.locmem.LocMemCache",
}
}
Django provides a pagination framework that can divide a large collection of objects into smaller pages.
from django.core.paginator import Paginator
paginator = Paginator(
students,
10
)
page = paginator.get_page(1)
Pagination is useful when displaying large numbers of records.
Django includes internationalization and localization features that help applications support multiple languages, date formats, number formats, and related locale-specific behavior.
Translation strings can be marked in templates and Python code for translation.
{% translate "Welcome" %}
Django provides testing tools that integrate with Python's testing ecosystem.
Developers can write tests for views, models, forms, URLs, and other application behavior.
from django.test import TestCase
class HomeTest(TestCase):
def test_home(self):
response = self.client.get("/")
self.assertEqual(
response.status_code,
200
)
Django encourages developers to organize functionality into applications that can be reused within projects and, when designed appropriately, shared across projects.
For example, a project can have separate applications for:
Django can be used to build APIs. Django REST Framework is a popular third-party toolkit for building Web APIs with Django.
This makes Django suitable as a backend for:
Django provides a broad collection of tools for developing modern web applications.
Question: Which Django feature allows developers to work with database records using Python models and QuerySets?