Sobes.tech
Middle

How would you describe the Django design model?

sobes.tech AI

Answer from AI

MVC (Model-View-Controller), but often described as MTV (Model-Template-View).

  • Model - defines the data structure, interacts with the database.
  • Template - responsible for presenting data to the user (HTML, JINJA2).
  • View - handles requests, retrieves data from the Model, passes it to the Template for rendering, and returns the response to the user.

Difference from the classic MVC: in Django, the View takes on the functions of the Controller, and the Template acts as the View.

# Example: views.py (View in Django terminology)
from django.shortcuts import render
from .models import Product

def product_list(request):
    products = Product.objects.all() # Interaction with Model
    return render(request, 'products/product_list.html', {'products': products}) # Passing data to Template

# Example: models.py (Model)
from django.db import models

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

    def __str__(self):
        return self.name