← Back to Django Course | Chapter 1: Django Fundamentals & Setup | Lesson 9 of 9

Django का MVT Architecture

MVT Django का jobs को तीन teams में बांटने का तरीका है: एक team data store करती है, एक team तय करती है क्या करना है, और एक team page दिखाती है।

MVT का मतलब क्या है

MVT का मतलब है Model, View, Template — वे तीन layers जिनमें Django एक web application को अलग करता है, हर एक की एक clear responsibility है।

उदाहरण: What MVT Stands For

One view touches all three MVT layers: Article.objects.all() reads the Model, the function itself is the View logic, and render() hands the data to the Template.

markup
# views.py
from django.shortcuts import render
from .models import Article

def article_list(request):
    articles = Article.objects.all()                          # Model
    return render(request, 'article_list.html', {'articles': articles})  # View + Template
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Model Layer

Model layer आपकी data structure define करती है और database से बात करती है; यह वह जगह है जहां आप describe करते हैं कि आपका app कौन सी information store करता है, जैसे एक blog post या एक user।

उदाहरण: The Model Layer

This Model layer code defines exactly what an Article is and how it's stored — Django turns this class into a real database table.

markup
# models.py
from django.db import models

class Article(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()
    published = models.DateTimeField(auto_now_add=True)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

View Layer

View layer में वह logic होता है जो तय करता है कि एक request के response में कौन सी data fetch करनी है और कौन सा template render करना है — यह models और templates को जोड़ने वाला "brain" है।

उदाहरण: The View Layer

The View layer's job is decision-making: here it decides to fetch only published articles, then names article_list.html as the Template that will display them.

markup
# views.py
from django.shortcuts import render
from .models import Article

def published_articles(request):
    articles = Article.objects.filter(published=True)
    return render(request, 'article_list.html', {'articles': articles})
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Template Layer

Template layer वह HTML file है जिसमें placeholders होते हैं जो Django असली data से भरता है, control करते हुए कि page user को exactly कैसा दिखे।

उदाहरण: The Template Layer

This is the Template layer: it never touches the database directly, it only loops over the articles the View already fetched and renders them as HTML.

markup
<!-- article_list.html -->
{% for article in articles %}
  <h2>{{ article.title }}</h2>
  <p>{{ article.body }}</p>
{% endfor %}
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

MVT vs Traditional MVC

Django का MVT MVC जैसा है, लेकिन Django खुद requests को views पर route करके Controller की तरह काम करता है, इसलिए developers सिर्फ Model, View, और Template parts लिखते हैं।

Note: Django को कभी-कभी "MVC, लेकिन Django Controller आपके लिए handle करता है" के रूप में describe किया जाता है।

उदाहरण: MVT vs Traditional MVC

In traditional MVC you write a Controller to route requests; in Django, urls.py plus the framework's own request handling does that job for you — you only write the Model, View, and Template parts.

markup
# urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('articles/', views.article_list),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Django के MVT pattern को दूसरे frameworks द्वारा उपयोग किए जाने वाले traditional MVC pattern के साथ confuse करना।
  2. Business logic को views के बजाय सीधे templates के अंदर रखना।
  3. यह सोचना कि Django MVT में "view" वह है जो user देखता है, जब यह असल में controller-जैसा logic layer है।
चैप्टर सारांश
  • MVT का मतलब है Model, View, Template।
  • Model data manage करता है और database से बात करता है।
  • View में वह logic होता है जो तय करता है कौन सी data भेजनी है।
  • Template control करता है वह data HTML के रूप में कैसे display हो।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.