Django Framework Architecture


Django’s framework architecture follows a variation of the traditional MVC (Model-View-Controller) pattern, but it is known as MTV (Model-Template-View) in Django’s terminology. The separation of concerns in Django ensures that different components (data, logic, and presentation) are handled in a structured and organized manner.

Django’s MTV architecture:

1. Model:

  • Role: The Model handles the database structure and data-related logic in the application.
  • What it does:
    • Defines the data models (database tables) using Python classes.
    • Interacts with the database to perform CRUD operations (Create, Read, Update, Delete).
    • Django’s Object-Relational Mapping (ORM) allows developers to write database queries using Python without writing raw SQL.

 

  • Example: A Product model in an e-commerce app that defines fields like name, price, and description.

 

from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=255)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    description = models.TextField()

 

2. Template:

  • Role: The Template layer is responsible for the presentation and user interface. It controls how the data is displayed to the end-user.
  • What it does:
    • It consists of HTML files mixed with Django Template Language (DTL), allowing developers to render dynamic content.
    • Templates are responsible for taking data from the view and displaying it in a structured and formatted way.
    • They use template tags and filters to include dynamic elements, like displaying variables or performing basic logic.

 

  • Example: Displaying a list of products using a template.

 

html

<h1>Product List</h1>
<ul>
    {% for product in products %}
        <li>{{ product.name }} - ${{ product.price }}</li>
    {% endfor %}
</ul>

 

3. View:

  • Role: The View handles the business logic of the application. It processes user requests, interacts with the models to retrieve or update data, and sends the response to the template.
  • What it does:
    • Receives HTTP requests and determines what to display based on the request.
    • Retrieves the necessary data from the model and sends it to the template.
    • Handles actions such as form submission, data retrieval, and sending responses (either as HTML, JSON, etc.).

 

  • Example: A view that gets all the products from the database and renders them in a template.

 

python

from django.shortcuts import render
from .models import Product

def product_list(request):
    products = Product.objects.all()
    return render(request, 'product_list.html', {'products': products})

 

4. URL Routing (Controller in MVC):

  • While Django uses the term View for the logic layer, the URL dispatcher acts similarly to the Controller in traditional MVC. It maps URLs to views.
  • What it does:
    • Defines URL patterns and routes requests to the appropriate view functions.
    • Uses regular expressions or path converters to specify dynamic parts of the URL.

 

  • Example: A URL pattern that maps the /products/ URL to the product_list view.

 

python

from django.urls import path
from . import views

urlpatterns = [
    path('products/', views.product_list, name='product_list'),
]

 

Workflow of MTV Architecture:

  1. Request Handling:

    • A user sends an HTTP request to the server (e.g., by accessing a URL).
    • Django’s URL dispatcher (defined in urls.py) receives the request and maps the URL to the appropriate View.
  2. Business Logic in View:

    • The View function processes the request. It interacts with the Model to retrieve or modify data (e.g., querying the database).
  3. Data Rendering with Template:

    • After getting the required data from the model, the view sends the data to the Template for rendering.
    • The template processes the data and generates the final HTML output.
  4. Response:

    • The generated HTML is sent back to the browser as the HTTP response, and the user sees the rendered page.

Diagram of Django’s MTV Architecture:

text

User Request  -->  URL Routing  -->  View  -->  Model (Database)
                                 |    |
                           View <-- Template (Render HTML)
                                  |
                           HTTP Response (HTML)

 

Benefits of Django’s MTV Architecture:

  1. Separation of Concerns: The separation between models, views, and templates makes the code more modular, reusable, and easier to maintain.

  2. Scalability: Each component can be scaled or modified independently (e.g., changing the database schema without affecting the view logic).

  3. Reusability: Models and templates can be reused in different parts of the application, reducing code duplication.

Django’s MTV architecture simplifies web development by keeping data handling, business logic, and presentation separate yet efficiently interconnected.

Django Framework Architecture


Enroll Now

  • Python Programming
  • Web Development