Django MVT
Django MVT
MVC Pattern
When talking about applications that provides UI (web or desktop), we usually
talk about MVC architecture. And as the name suggests, MVC pattern is based on
three components: Model, View, and Controller.
Django is written in 100% pure Python code, so you'll need to install Python on
your system. Latest Django version requires Python 2.6.5 or higher
If you're on one of the latest Linux or Mac OS X distribution, you probably
already have Python installed. You can verify it by typing python command at a
command prompt. If you see something like this, then Python is installed.
$ python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
MVT:
Django is based on MVT (Model-View-Template) architecture. MVT is a software
design pattern for developing a web application.
Model: The model is going to act as the interface of your data. It is responsible for
maintaining data. It is the logical data structure behind the entire application and is
represented by a database (generally relational databases such as MySql, Postgres).
A Django model is a Python class that represents a database table. Models make it easy
to define and work with database tables using simple Python code. Instead of writing
complex SQL queries, we use Django’s built-in ORM (Object Relational Mapper),
which allows us to interact with the database in a more readable and organized way.
Key benefits of using Django models:
Simplifies database operations like creating, updating, and deleting.
Automatically generates SQL queries.
Integrates with Django’s admin interface.
Provides built-in validations and metadata handling.
Example
from django.db import models
class GeeksModel(models.Model):
title = models.CharField(max_length = 200)
description = models.TextField()
This program defines a Django model called "GeeksModel" which has two fields:
title: A character field with a 200-character limit.
description: A text field for longer input.
This model creates a corresponding table in the database when migrations are applied.
Django maps the fields defined in Django models into table fields of the database as
shown below.
View: The View is the user interface — what you see in your browser when you render
a website. It is represented by HTML/CSS/Javascript and Jinja files.
Template: A template consists of static parts of the desired HTML output as well as
some special syntax describing how dynamic content will be inserted.
Project Structure :
A Django Project when initialized contains basic files by default such as manage.py,
view.py, etc. A simple project structure is enough to create a single-page application.
Here are the major files and their explanations. Inside the geeks_site folder ( project
folder ) there will be the following files-
manage.py- This file is used to interact with your project via the command line(start
the server, sync the database... etc). For getting the full list of commands that can be
executed by manage.py type this code in the command window-
$ python manage.py help
folder ( geeks_site ) - This folder contains all the packages of your project. Initially, it
contains four files -
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'projectApp'
]
We have finally created an app but to render it using urls we need to include the app in
our main project so that urls redirected to that app can be rendered.
4. Include App URLs in the Main Project
Edit projectName/urls.py to include your app’s URLs:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("projectApp.urls")), # Routes all other URLs to projectApp
]
Make sure you create a urls.py file inside projectApp to define your app’s routes.
Using Django’s MVT Model
Once your project is set up:
Models: Define your data structure in projectApp/models.py.
Views: Create functions or class-based views in projectApp/views.py to handle
requests.
Templates: Create HTML templates in a templates folder (either in the app or at
the project level) and use Django’s templating language to render dynamic
content.