Django का MVT Architecture
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.
# 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.
# 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.
# 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.
<!-- 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 लिखते हैं।
उदाहरण: 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.
# 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. #}
- Django के MVT pattern को दूसरे frameworks द्वारा उपयोग किए जाने वाले traditional MVC pattern के साथ confuse करना।
- Business logic को views के बजाय सीधे templates के अंदर रखना।
- यह सोचना कि Django MVT में "view" वह है जो user देखता है, जब यह असल में controller-जैसा logic layer है।
- MVT का मतलब है Model, View, Template।
- Model data manage करता है और database से बात करता है।
- View में वह logic होता है जो तय करता है कौन सी data भेजनी है।
- Template control करता है वह data HTML के रूप में कैसे display हो।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: