Architecture of django (MVT)

Jan. 2, 2023


0
5 min read
627

The architecture of Django is based on the Model-View-Template (MVT) design pattern. In this pattern, the "Model" represents the data and business logic of the application, the "View" represents the user interface, and the "Template" represents the structure and layout of the user interface.

Model

In the MVT pattern, the "Model" represents the data and business logic of the application. It is responsible for managing the data, performing calculations and manipulations on the data, and communicating with the database.

In Django, a model is a class that represents a table in a database. It is used to define the structure of the data that will be stored in the database, including the fields or columns of the table and any relationships with other tables.

Models are defined in Django using Python classes that are derived from django.db.models.Model. Each model class defines a set of fields, each of which represents a database column, along with various options and behaviors.

Here is an example of a simple model class in Django:

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    age = models.IntegerField()

This model defines a Person class with three fields: first_name, last_name, and age. Each field is represented by a specific field type, such as CharField for character fields and IntegerField for integer fields.

Once you have defined your models, Django provides a variety of tools for working with the data stored in the database, including a powerful ORM (Object-Relational Mapper) that allows you to query the database using Python syntax.

View

The "View" represents the user interface of the application. It is responsible for rendering the user interface and handling user input.

In Django, a view is a Python function that takes a web request and returns a web response. The web request is represented by an object called HttpRequest, and the web response is represented by an object called HttpResponse.

Views are responsible for performing the logic of your web application. This can include fetching data from the database, processing user input, and rendering templates.

In Django, views are defined in the views.py file of your app. Here is an example of a simple view function in Django:

from django.http import HttpResponse

def greetings(request):
    return HttpResponse("Hello, World!")

This view function takes a request object as an argument and returns a simple HTTP response with the text "Hello, World!".

To map a view function to a URL pattern, you will need to use Django's URL dispatcher. The URL dispatcher allows you to specify the URL patterns that should trigger a particular view function.

For example, you might define a URL pattern like this:

from django.urls import path

urlpatterns = [
    path('greetings/', greetings),
]

This URL pattern will map the greetings view function to the URL /greetings/. When a user navigates to this URL in their web browser, the view function will be triggered and the response returned by the function will be sent back to the user.

Template

The "Template" represents the structure and layout of the user interface. It is a text file that contains the necessary HTML, CSS, and JavaScript code to render the desired user interface.

In Django, a template is a text file that contains the necessary HTML, CSS, and JavaScript code to render the desired user interface. Templates allow you to define the structure and layout of a web page or section of a web page, and to include placeholders for dynamic content.

Django uses a templating language called Django Template Language (DTL) to define the structure and content of templates. DTL is a simple, yet powerful language that allows you to define the layout of a template using a combination of static HTML and Django template tags.

Here is an example of a simple template in Django:

<html>
<head>
  <title>My Site</title>
</head>
<body>
  <h1>Welcome to my site!</h1>
  <p>My name is {{ name }} and I am {{ age }} years old.</p>
</body>
</html>

This template includes a title and a header element, as well as a paragraph element that contains two placeholders: {{ name }} and {{ age }}. These placeholders will be replaced with the actual values when the template is rendered.

To render a template in a Django view, you can use the render() function provided by Django. The render() function takes a request object, the name of the template to be rendered, and a context dictionary containing the values to be used in the template.

Here is an example of how you might use the render() function in a Django view:

from django.shortcuts import render

def greetings(request):
    context = {
        'name': 'John',
        'age': 30,
    }
    return render(request, 'greetings.html', context)

This view function will render the greetings.html template and send the resulting HTML back to the user as an HTTP response. The context dictionary will be used to populate the placeholders in the template with the appropriate values.

This architecture allows Django to handle requests and responses efficiently and to separate the different aspects of web development, which can make it easier to maintain and modify the application over time.

django Python MVT Architecture-of-Django Appreciate you stopping by my post! 😊

Add a comment


Note: If you use these tags, write your text inside the HTML tag.
Login Required
Author's profile
Profile Image

Abdulla Fajal

Django Developer

With 'espere.in' under my care, I, Abdulla Fajal, graciously invite your insights and suggestions, as we endeavour to craft an exquisite online experience together.