0% found this document useful (0 votes)
2 views10 pages

Django MVT

Django is a high-level Python web framework that facilitates rapid development of secure and maintainable web applications, featuring built-in functionalities like user authentication and an admin panel. It follows the Model-View-Template (MVT) architecture, where Django manages the controller aspect, allowing developers to focus on models and templates. The document outlines the installation process for Django on various operating systems, the structure of a Django project, and how to create and manage applications within the framework.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Django MVT

Django is a high-level Python web framework that facilitates rapid development of secure and maintainable web applications, featuring built-in functionalities like user authentication and an admin panel. It follows the Model-View-Template (MVT) architecture, where Django manages the controller aspect, allowing developers to focus on models and templates. The document outlines the installation process for Django on various operating systems, the structure of a Django project, and how to create and manage applications within the framework.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Django

Django is a Python-based web framework which allows us to quickly develop robust


web application without much third party installations. It comes with many built-in
features—like user authentication, an admin panel and form handling—that help you
build complex sites without worrying about common web development tasks. Django is
a high-level Python web framework that enables rapid development of secure and
maintainable websites. Built by experienced developers, Django takes care of much of
the hassle of web development, so you can focus on writing your app without needing to
reinvent the wheel. It is free and open source, has a thriving and active community, great
documentation, and many options for free and paid-for support.
Key Features of Django
 Rapid Development: Build fully featured web applications quickly.
 Built-In Admin Interface: Manage your app’s data easily through an automatically
generated admin panel.
 Database Flexibility: Easily switch between databases
like SQLite, MySQL or PostgreSQL.
 Extensible: Thousands of additional packages are available to extend Django’s
capabilities.
 Scalability: Designed to grow with your application.

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 MVC - MVT Pattern


 The Model-View-Template (MVT) is slightly different from MVC. In fact the main
difference between the two patterns is that Django itself takes care of the
Controller part (Software Code that controls the interactions between the Model
and View), leaving us with the template. The template is a HTML file mixed with
Django Template Language (DTL).
 The following diagram illustrates how each of the components of the MVT
pattern interacts with each other to serve a user request −
 The developer provides the Model, the view and the template then just maps it to
a URL and Django does the magic to serve it to the user.
 Django development environment consists of installing and setting up Python,
Django, and a Database System. Since Django deals with web application, it's
worth mentioning that you would need a web server setup as well.

Step 1 Installing Python

 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

Step 2 - Installing Django


Installing Django is very easy, but the steps required for its installation depends on your
operating system. Since Python is a platform-independent language, Django has one
package that works everywhere regardless of your operating system.
You can download the latest version of Django from the
link http://www.djangoproject.com/download.
UNIX/Linux and Mac OS X Installation
You have two ways of installing Django if you are running Linux or Mac OS system −
 You can use the package manager of your OS, or use easy_install or pip if
installed.
 Install it manually using the official archive you downloaded before.
We will cover the second option as the first one depends on your OS distribution. If you
have decided to follow the first option, just be careful about the version of Django you
are installing.
Let's say you got your archive from the link above, it should be something like Django-
x.xx.tar.gz:
Extract and install.
$ tar xzvf Django-x.xx.tar.gz
$ cd Django-x.xx
$ sudo python setup.py install
You can test your installation by running this command −
$ django-admin.py --version
If you see the current version of Django printed on the screen, then everything is set.
Note − For some version of Django it will be django-admin the ".py" is removed.
Windows Installation
We assume you have your Django archive and python installed on your computer.
First, PATH verification.
On some version of windows (windows 7) you might need to make sure the Path system
variable has the path the following C:\Python34\;C:\Python34\Lib\site-packages\
django\bin\ in it, of course depending on your Python version.
Then, extract and install Django.
c:\>cd c:\Django-x.xx
Next, install Django by running the following command for which you will need
administrative privileges in windows shell "cmd" −
c:\Django-x.xx>python setup.py install
To test your installation, open a command prompt and type the following command −
c:\>python -c "import django; print(django.get_version())"
If you see the current version of Django printed on screen, then everything is set.
OR
Launch a "cmd" prompt and type python then −
c:\> python
>>> import django
>>> django.VERSION
Step 3 Database Setup
Django supports several major database engines and you can set up any of them based
on your comfort.
 MySQL (http://www.mysql.com/)
 PostgreSQL (http://www.postgresql.org/)
 SQLite 3 (http://www.sqlite.org/)
 Oracle (http://www.oracle.com/)
 MongoDb (https://django-mongodb-engine.readthedocs.org)
 GoogleAppEngine Datastore
(https://cloud.google.com/appengine/articles/django-nonrel)
You can refer to respective documentation to installing and configuring a database of
your choice.
Note − Number 5 and 6 are NoSQL databases.
Step 4 Web Server
Django comes with a lightweight web server for developing and testing applications.
This server is pre-configured to work with Django, and more importantly, it restarts
whenever you modify the code.

MVT:
Django is based on MVT (Model-View-Template) architecture. MVT is a software
design pattern for developing a web application.

MVT Structure has the following three parts -

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 -

 _init_.py - It is a python package. It is invoked when the package or a module in


the package is imported. We usually use this to execute package initialization
code, for example for the initialization of package-level data.
 settings.py - As the name indicates it contains all the website settings. In this file,
we register any applications we create, the location of our static files, database
configuration details, etc.
 urls.py - In this file, we store all links of the project and functions to call.
 wsgi.py - This file is used in deploying the project in WSGI. It is used to help your
Django application communicate with the webserver.
Django Architecture: The MVT Pattern
Django is based on MVT (Model-View-Template) architecture. MVT is a software design
pattern for developing a web application. It's structure has the following three parts :
1. Model: Acts as the data interface. It defines the structure of your data and is usually
backed by a database (e.g., MySQL, PostgreSQL).
2. View: A Python function or class that handles web requests. It interacts with the
Model and renders a response, typically by passing data to a Template.
3. Template: Contains static HTML mixed with Django’s templating syntax. Templates
are used to display dynamic data on the web page.
To check more about Django's architecture, visit Django Project MVT Structure
Installing Django
Follow these steps to set up Django on your system:
1. Install Python 3:
Download and install the latest Python 3 version from the official website.
2. Install pip:
Pip comes with recent versions of Python. Open the command prompt and run:
pip --version
3. Set Up a Virtual Environment:
This isolates your project’s dependencies.
python -m virtualenv env_site
4. Activate the Environment:
Windows:
cd env_site\Scripts\activate
Mac/Linux:
source env_site/bin/activate
5. Install Django:
With your virtual environment active, run:
pip install django
Create a Django Project and App
1. Create a Django Project
Open the terminal and run:
django-admin startproject projectName
cd projectName
This creates a new folder named projectName containing your project settings and
then changes the current working directory to it..
2. Create a Django App
Inside the project directory (where manage.py is located), run:
python manage.py startapp projectApp
This creates a new app named projectApp.

Now you can see your directory structure as under :


3. Update INSTALLED_APPS in Settings
In projectName/settings.py, add your app to the INSTALLED_APPS list:
# Application definition

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.

You might also like