Junior — Middle
Can you explain what the Model-View-Template (MVT) pattern is in Python development?
sobes.tech AI
Answer from AI
The Model-View-Template (MVT) pattern is an architectural pattern used in Python web frameworks, such as Django.
- Model — responsible for data handling and business logic, usually represented by classes that interact with the database.
- View — processes user requests, retrieves data from the Model, and passes it to the Template.
- Template — responsible for displaying data to the user, generating HTML pages.
Example: a user makes a request, the View retrieves data from the Model, then passes it to the Template for display.
# views.py
from django.shortcuts import render
from .models import Product
def product_list(request):
products = Product.objects.all()
return render(request, 'products/list.html', {'products': products})
Here, the View gets data from the Model and passes it to the Template for rendering.