Unit 6
Unit 6
Unit 6
• Django is an open source web application framework for developing web applications
in Python.
• Thus, web applications built with Django can work with different databases without
requiring any code changes.
• With this flexibility in web application design combined with the powerful capabilities
of the Python language and the Python ecosystem, Django is best suited for cloud
applications.
• Model
• The model acts as a definition of some stored data and handles the interactions with
the database. In a web application, the data can be stored in a relational database,
non-relational database, an XML file, etc. A Django model is a Python class that
outlines the variables and methods for a particular type of data.
Django Architecture
• Template
• In a typical Django web application, the template is simply an HTML page with
a few extra placeholders. Django’s template language can be used to create
various forms of text files (XML, email, CSS, Javascript, CSV, etc.).
• View
• The view ties the model to the template. The view is where you write the code that
actually generates the web pages. View determines what data is to be displayed,
retrieves the data from the database and passes the data to the template.
Django Framework
Starting Development with Django:
When you create a new Django project, a number of files are created as shown below:
8
Strating developing with Django
django-admin.py startproject
<PROJECT_ROOT>
manage.py : This file contains an arry of functions for managing the site
<PROJECT_DIR>
init .py : This file tells phython that this folder is phyton package
settings.py : This file contains the website’s settings
urls.py : This file contains the URL patterns that map URLs to pages
Django project can have multiple applications.
-Apps are where you write the code that makes your website function.
-Each project have multiple apps and each app can be part of multiple projects
When a new application is created a new directory for the application is also
created which has a number of files such as,
• Model.py : This file contains the description of the model for the application
• View. Py : This file contains the application views.
Configuring a Database:
Developers have a wide choice of databases that can be used for web applications
including both relational and non-relational databases.
Django provides a unified API for database backbends thus giving the freedom to
choose the database.
12
Django Framework :Starting Development with Django
Configuring MySQL database with Django – settings.py
@DATABASE
dictionary
13
Django Framework :Starting Development with Django
Defining a Model: Model acts as a definition of the data in the database
1
2
https://github.com/Oskube/django-dht22/blob/master/dht22/models.py
https://thedig95.github.io/2018/09/08/data-api.html 3
Django provides an administration system that allows to manage the website without writing additional code.
“admin” system reads the Django model and provides an interface that can be used to add content to site
To define the editable application models in the admin interface, a new file named admin.py
is to be created.
14
Django Framework :Starting Development with Django
Django Admin site:
Adding new items in the temperature data table using admin site:
Reference: https://djangobook.com/mdj2-django-admin/
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-objects
16
Django Framework :Starting Development with Django
Django Admin site:
Reference: https://djangobook.com/mdj2-django-admin/
https://docs.djangoproject.com/en/3.0/intro/tutorial02/
https://data-flair.training/blogs/django-admin-interface/
17
Django Framework :Starting Development with Django
Defining a View:
The view contains the logic that glues the model to the template.
The view determines the data to be displayed in the template, retrieves the data from the Database and
passes it to the template
The view also extracts the data posted in a form in the template and inserts it in the
database
Each page in the website has a separate view, and is basically a Python function in the views.py file
Views can also perform additional tasks such as authentication, sending emails etc.
18
Django Framework :Starting Development with Django
Defining a View:
The view contains the logic that glues the model to the template.
The view determines the data to be displayed in the template, retrieves the data
from the Database and passes it to the template
The view also extracts the data posted in a form in the template and inserts it in the
database
Each page in the website has a separate view, and is basically a Python function in
the views.py file
Views can also perform additional tasks such as authentication, sending emails etc.19
Django Framework :Starting Development with Django
Defining the Django URL patterns:
URL patterns are a way of mapping the URLs to the views that should handle the URL requests
URLs requested by the user are matched with the URL patterns
The view corresponding to the pattern matches the URL is used to handle the request.
20
Django Framework :Running a Django Application
With the Django Models, Views, Templates and URLs defined for the Django project, the application will finally
run with commands as shown below: