The Django web framework has a model-view-template (MVT) architecture, which makes it the only framework you’ll need to create a complete website or web application. This Python framework allows you to create models that generate databases and render dynamic HTML templates to the UI using views.

The power of Django is no secret; it's fast, reliable, scalable, and secure. The reliability, as well as the scalability of this software, relies on its MVT architecture. And in this article, you’ll learn exactly how Django’s MVT architecture works.

What Is Django’s Model?

The model in Django’s MVT architecture defines the structure and behavior of the data you want to store through your website. Each Django model you create generates a corresponding database table, where each attribute of the model becomes a field in the table.

Continuing with the setup from our introductory article on Django, you can create a model for the sellers. A seller can have a seller model that has personal information, such as a name and contact details, and a related model for the items each seller sells.

The Existing Sample Django Project File Structure

        mysite/
 mysite/
   _pycache_
   _init_.py
   asgi.py
   settings.py
   urls.py
   wsgi.py
 sellers/
   migration
   _init_.py
   admin.py
   apps.py
   models.py
   test.py
   views.py
 db.sqlite3
 manage.py

Creating Django Models

If you look under the sellers’ app section in the file structure above, you’ll see a file called models.py. This is where you’ll create all your Django models for the sellers’ section of your website. Each model that you create will be a subclass of Django’s Model API, which is why each Django-generated models.py file has a default models import.

The models.py File

        From django.db import models



# Create your models here.



class Seller(models.Model):

first_name = models.CharField(max_length=30)

last_name = models.CharField(max_length=30)

contact_number = models.CharField(max_length=30)



class Product(models.Model):

seller = models.ForeignKey(Seller, on_delete=models.CASCADE)

item_name = models.CharField(max_length=100)

item_qantity = models.IntegerField()

item_price = models.DecimalField(max_digits=9, decimal_places=2)

item_description = models.TextField()

The code above is a copy of the updated content of the model.py file. The file now creates two models—Seller and Product. These models share a one-to-many relationship, where one seller can have many products on sale. So, the Product model has a foreign key from the seller and an on_delete attribute set to models.CASCADE, which means that when you delete a seller, you'll automatically delete every product that has that primary key as a foreign key.

Related: Learn Python and Django Today With These Amazing Courses You might also notice that each of the models in the code above doesn’t have a primary key. This is because Django will automatically generate a primary key if you don’t explicitly create one.

Before you can use any model that you create, you’ll need to tell Django where to find it. To do this, you’ll need to navigate to the settings.py file and insert the name of the module that contains the models.py file, into the INSTALLED_APP section.

In the sample project for this article, the models.py file is in the sellers’ module. Therefore, the updated INSTALLED_APP section will read as follows:

        INSTALLED_APPS = [

'django.contrib.admin',

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.messages',

'django.contrib.staticfiles',

'sellers',

]

With the code above, the models in the sellers’ app are now visible to the Django website, and now you can move on to migrations.

Conducting migrations is important because this process is a way of spreading the changes you make in your models to the corresponding database schema. So, every time you make changes to your model, you’ll need to conduct the process of migration—which involves two steps.

Step one is to make migrations, which is a command that creates migrations based on the changes detected in the models.py file. To start the migration process, you’ll need to open your terminal, navigate to the directory that has your Django project, and launch the server using the following command:

        python manage.py runserver

With the server running in one terminal, open a new terminal and enter the following command:

        python manage.py makemigrations

After execution, the terminal will produce the following output:

        Migrations for 'sellers':

sellers\migrations\0001_initial.py

- Create model Seller

- Create model Product

The output above clearly states that you now have migrations for two models—the seller and the product. Now if you navigate to the migration folder in your sellers’ module, you’ll see that it now has a new file called 0001_initial.py. Within this file, you’ll find the migrations you just created.

The 0001_initial.py File

        # Generated by Django 3.2.9 on 2022-02-26 16:06



from django.db import migrations, models

import django.db.models.deletion



class Migration(migrations.Migration):



initial = True



dependencies = [

]



operations = [

migrations.CreateModel(

name='Seller',

fields=[

('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),

('first_name', models.CharField(max_length=30)),

('last_name', models.CharField(max_length=30)),

('contact_number', models.CharField(max_length=30)),

],

),

migrations.CreateModel(

name='Product',

fields=[

('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),

('item_name', models.CharField(max_length=100)),

('item_qantity', models.IntegerField()),

('item_price', models.DecimalField(decimal_places=2, max_digits=9)),

('item_description', models.TextField()),

('seller', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='sellers.seller')),

],

),

]



Each time you make a new migration, the migration folder generates a new version of this file.

Step two of the migration process is to finally migrate the models. This means that you synchronize the database state with the models.py file, using the migrations that you just created in the 0001_initial.py file. You can complete this process (while the server is still running) with the following command:

        python manage.py migrate

What Is Django’s Template?

Templates are a way to dynamically generate HTML for your Django project. Each Django template has the .html extension and a combination of static and dynamic content. Django templates have a unique syntax that includes new ways of creating variables and tags in an HTML document.

Creating A Django Template

To introduce templates in the sample Ecommerce website for this article, you’ll need to create a new directory in the sellers’ module. This new directory called "templates" will be the home of all HTML documents for the sellers’ app—starting with the home page.

The sellers_home.html File

        <!DOCTYPE html>

<html lang="en">

<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Sellers | E-commerce</title>

</head>

<body>

<h1>Welcome Sellers!</h1>

</body>

</html>

After you’ve created your templates, you’ll need to make them visible to Django by adding your templates' directory to the TEMPLATES section of the settings.py file. The updated TEMPLATES section will look like the following:

        TEMPLATES = [

{

'BACKEND': 'django.template.backends.django.DjangoTemplates',

'DIRS': [

# new code that points to the location of the templates

BASE_DIR / 'sellers' / 'templates'

],

'APP_DIRS': True,

'OPTIONS': {

'context_processors': [

'django.template.context_processors.debug',

'django.template.context_processors.request',

'django.contrib.auth.context_processors.auth',

'django.contrib.messages.context_processors.messages',

],

},

},

]

Now that Django knows where to find the templates for the website, you can go ahead and render them to the UI using the view.

What Is Django’s View?

The View is the second layer of Django’s MVT architecture, a template is useless unless a view renders it to the UI. The view is responsible for accepting web requests and returning appropriate responses (including templates). In its most basic form, the view is a Python function, stored in the view.py file of your Django project.

Creating Django View

The view.py file is in the sellers’ module of the sample Django project. When a seller visits your website, you’ll want them to go to a sellers’ home page. This home page you’ll create using an HTML template, much like the one created in the template section above.

The view.py File

        from django.shortcuts import render



def index(request):

return render(request, 'sellers_home.html')

The view above takes a request and returns the sellers’ HTML template. So, each time a user visits (or requests) http://127.0.0.1:8000/sellers/ they’ll see the sellers’ home page. This is after you create a urls.py file in the sellers’ module.

The Sellers urls.py File

        from django.urls import path

from . import views



urlpatterns = [

path('', views.index, name='index'),

]

And include the path to the sellers’ module urls.py file in the urls.py file located in the main Django directory.

The Website urls.py File

        from django.contrib import admin

from django.urls import include, path



urlpatterns = [

path('sellers/', include('sellers.urls')),

path('admin/', admin.site.urls),

]

Now that the view is set up, you can ensure that the Django server is still running and navigate to http://127.0.0.1:8000/sellers/ in your browser to see the sellers’ home page.

The Sellers’ Home Page

Sellers home page

Django’s MVT Architecture vs. MVC Architecture

Django’s MVT architecture is quite different from the popular MVC architecture.

The template section of the MVT architecture operates in much the same way as the view in the MVC architecture, while the view in the MVT architecture displays qualities that are similar to the controller in the MVC architecture. However, the models in both architectures work identically.