Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

Explain Django MVT (Model-View-Template) architecture and how it differs from MVC. Django MVT architecture क्या है और MVC से कैसे अलग है?

Answer

MVT (Model-View-Template) is Django's architectural pattern where Model manages data, View handles logic, and Template generates HTML. Unlike MVC, Django's View is the Controller and Template is the View, making MVT a variation optimized for Django's design philosophy.

ComponentMVT (Django)MVC (Rails)Responsibility
ModelDatabase layerDatabase layerData & business logic
View/ControllerView = Business logicController = LogicProcess requests
Template/ViewTemplate = UIView = UIRender HTML
// DJANGO MVT FLOW
// 1. URL Routes to View
// 2. View retrieves/processes data from Model
// 3. View renders Template with data
// 4. Template displays HTML

// models.py (Model Layer)
from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    created_at = models.DateTimeField(auto_now_add=True)
    
    def __str__(self):
        return self.name

// views.py (View Layer - Business Logic)
from django.shortcuts import render, get_object_or_404
from .models import Product

def product_list(request):
    # Get data from Model
    products = Product.objects.all()
    # Process data
    total_price = sum(p.price for p in products)
    # Pass to Template
    context = {'products': products, 'total': total_price}
    return render(request, 'product_list.html', context)

def product_detail(request, pk):
    product = get_object_or_404(Product, pk=pk)
    return render(request, 'product_detail.html', {'product': product})

// urls.py (URL Routing)
from django.urls import path
from . import views

urlpatterns = [
    path('products/', views.product_list, name='product_list'),
    path('products/<int:pk>/', views.product_detail, name='product_detail'),
]

// product_list.html (Template Layer - UI)
{% for product in products %}
    <div class='product'>
        <h2>{{ product.name }}</h2>
        <p>Price: ${{ product.price }}</p>
        <a href='{% url "product_detail" product.pk %}'>
            View Details
        </a>
    </div>
{% endfor %}
<p>Total: ${{ total }}</p>

// MVT Request-Response Cycle
// 1. User requests /products/
// 2. Django matches URL pattern
// 3. View retrieves Products from Model
// 4. View passes data to Template
// 5. Template renders HTML
// 6. HTML sent back to user

// Advantages of MVT
// 1. Clear separation of concerns
// 2. Reusable components
// 3. Easy to test each layer
// 4. Rapid development
// 5. Secure by default
MVT Architecture:

Model (Database Layer):
- Define डेटा structures
- Database queries
- Business logic

View (Logic Layer):
- Process requests
- Retrieve data from Model
- Call Template with data

Template (UI Layer):
- HTML rendering
- Dynamic content display
- Form rendering

Flow:
User request -> URL Router -> View
             -> Model (data) -> View
             -> Template (render) -> Response

Difference from MVC:
MVC में: Model, Controller, View
MVT में: Model, View (= Controller), Template (= View)

Benefit:
Clean code, reusability, testing easy

Was this answer clear?