Lesson 3 of 60 – Django Features
5%

Django Features

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.

Note: Django combines URL routing, views, templates, models, ORM, forms, authentication, security features, administration, and many other tools in one web framework.

1. High-Level Python Web Framework

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

2. Rapid Development

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.

Benefit: Developers can focus on application-specific functionality instead of repeatedly implementing common web features.

3. MTV Architecture

Django follows an architecture commonly described as Model-Template-View (MTV).

  • Model: Handles data and database representation.
  • Template: Handles presentation.
  • View: Handles request processing and application logic.
Model
  ↓
Database

View
  ↓
Application Logic

Template
  ↓
Presentation

4. Object-Relational Mapper

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)

5. Database Support

Django provides database backends that allow applications to work with supported relational database systems.

Commonly used database systems with Django include:

  • SQLite
  • PostgreSQL
  • MySQL
  • Oracle

Database configuration is normally defined in the DATABASES setting.

6. URL Routing

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.

7. Template System

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.

8. 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.

9. Built-in Admin Interface

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)

10. Authentication System

Django includes an authentication framework for managing users, passwords, groups, permissions, and sessions.

Authentication can be used for:

  • Login systems.
  • Logout systems.
  • User registration.
  • Protected pages.
  • User permissions.
  • Groups.

11. Authorization and Permissions

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

12. Forms

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
    )

13. Form Validation

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.

14. Security Features

Security is an important part of Django. The framework provides features that help developers address several common web security risks.

  • CSRF protection.
  • Clickjacking protection.
  • Host validation.
  • Secure password hashing.
  • Security-related middleware.
  • ORM protections against many common SQL injection patterns.
Important: Django's security features do not automatically make every application secure. Developers must configure and use the framework correctly.

15. Cross-Site Request Forgery Protection

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>

16. Middleware

Middleware provides hooks into Django's request and response processing.

Middleware can be used for features such as:

  • Security.
  • Sessions.
  • Authentication.
  • Messages.
  • CSRF protection.

Middleware is configured in the MIDDLEWARE setting.

17. Static Files

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>

18. File Uploads

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/"
    )

19. Image Upload Support

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.

20. Sessions

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.

21. Messages Framework

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.

22. Generic Views

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

23. Class-Based Views

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.

24. Caching Support

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",
    }
}

25. Pagination

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.

26. Internationalization

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" %}

27. Automated Testing Support

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
        )

28. Reusable Applications

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:

  • Students.
  • Courses.
  • Blog.
  • Payments.
  • Attendance.

29. Django REST API Ecosystem

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:

  • Web applications.
  • Mobile applications.
  • JavaScript frontends.
  • Other software systems.
Remember: Django itself is the web framework. Django REST Framework is a separate package built for API development.

30. Summary of Django Features

Django provides a broad collection of tools for developing modern web applications.

  • Python-based web framework.
  • MTV architecture.
  • URL routing.
  • Templates.
  • Template inheritance.
  • ORM and database support.
  • Admin interface.
  • Authentication and permissions.
  • Forms and validation.
  • Security features.
  • Middleware.
  • Static files.
  • File and image uploads.
  • Sessions.
  • Messages.
  • Generic and class-based views.
  • Caching.
  • Pagination.
  • Internationalization.
  • Testing support.
  • Reusable applications.
  • API development ecosystem.
Great! You now understand the major features that make Django a powerful framework for Python web development.

📌 Key Points

  • Django is a high-level Python web framework.
  • Django follows the Model-Template-View architecture.
  • Django includes an ORM for database operations.
  • Django provides URL routing and a template system.
  • Django has a built-in administration interface.
  • Django provides authentication, authorization, and sessions.
  • Django includes forms and validation.
  • Django provides several security mechanisms.
  • Django supports static files and file uploads.
  • Django provides generic views and class-based views.
  • Django includes caching and pagination support.
  • Django supports internationalization and localization.
  • Django provides tools for automated testing.
  • Django REST Framework can be used with Django to build Web APIs.

🧠 Quick Quiz

Question: Which Django feature allows developers to work with database records using Python models and QuerySets?