In this post, we will discuss the MVT pattern on Django.
The MVT (Model View Template) is a software design pattern. It is a collection of three important components Model View and Template. Although Django follows the MVC pattern but maintains its own conventions. So, control is handled by the framework itself.
Model
Basically Model act as Meditator Between the Website Interface and the Database. In Django, We use Python Classes To make Models Which is nothing but a Table in a Database. Class Attributes is Columns of the Table in the database and on these attributes we can set constraints like primary key, foreign key, Many to Many Relation, one to many Relation, etc. So using Models in Django makes it easy to connect with the database. or we can say that model is the whole logic of your project or business.
View
The View is used to execute the business logic and interact with a model to carry data and renders a template. The view can be a function-based view or can be Class-based view. when the user requests something there is basically a function or class is written to handle those requests. for example when the user request ‘www.yoursitename.com/home’. so basically here user requested for the home page of your site and after this, the view you have written to handle this particular request will return the response to the user in the form of an HTML file or we can say that HTTTP response.
Templates
In Django, Templates are nothing but the HTML file which handles the User Interface part completely. These Templates rendered when the user requests these templates. So basically templates are returned response by the view to the user requests.
So let's look at this in Picture
Here you can see that user requests to your Django site. user has written some URL (Uniform Resource Locator) like “www.yoursite_name.com/page_name” after this, URL will match to the specified View, Now View will check the request object if there are any attributes related to Database it will fetch from or save those attributes to the database. If there are no Attributes related to the Database simply it will look for an HTML page (templates) to return a response but if it doesn’t found any templates it will show the error “404 pages nor found”.
Thank You for Reading🙂. If you have any Suggestions or Query then let me know in the comment section.
Top comments (0)