Instagram
youtube
Facebook
Twitter

Django Architecture

Django MVT Pattern

There are mainly two types of software design patterns. the first one is MVC and the second one is MVT. MVC stands for model view controller and MVT stands for the model view template. 

MVC Pattern

In the MVC pattern, the model is the central component of this pattern which is responsible for managing data, handling Logic, etc. and the view deals with how the data will be displayed to the user, and the controller manipulates the model and renders the view by acting as a bridge between them. 

MVT Pattern

MVT is another Design pattern that is similar to the MVC pattern. This model works similarly to MVC. the view also executes the logic. But this design uses the template. template work as a presentation layer All the HTML code of the website renders through the template. Django also works on the MVT pattern.

How Django’s MVT Pattern Works

Whenever a user opens a website Django first searches for a URL to map. After the URL gets mapped it calls the view. Then, views interact with the models and templates. After this Django gets back to the user and renders the template.

 

Django Model

The Django model is a class that is used to store essential fields and methods. Each model has its table in the database. Django uses SQLite as its default database. A database is the collection of data. 

Let us understand the whole concept of the Django model with some practical work.

 

Creating a contact form in the Django model

Open the models.py file of your app and write the code given below.

class Contact(models.Model):
    name = models.CharField(max_length=50)
    email= models.CharField(max_length=50)
    phone = models.CharField(max_length=12)
    message= models.TextField()

Here, we have created a class in Django models. Contact is our model name. models.Model means our Contact is a model by, this Django knows that it should be saved in the database.

Name, email, phone, and message are our class attributes and every attribute refers to a different database column. Here, models. CharField indicates that our class attribute will store a limited number of characters and max_length refers to the maximum length. TextField is used to store text without a limit.

If you want to learn more about Django models please go through the official Django documentation(Django documentation)

 

Creating a table for our model in the database

To inform Django that we have made some changes in the database. Use the following command.

\Codersdaily\project1> python manage.py makemigrations

Now, Django has prepared a migration file for us. To apply that in the database use the given command.

\Codersdaily\project1> python manage.py migrate

So, our Django model is now successfully added to the database. But, a new question arises: how can we view the data saved in our Django model? That's where the concept of Django admin comes in.