Unit 6
IoT Physical Servers,
Cloud Offerings & IoT
Case Studies
Communication API
• Cloud Models are relied on Communication API
• Communication API facilitate data transfer, control information transfer from
application to cloud, one service to another
• It also exist in the form of Communication Protocols
• Eg. Popular API is RESTful API (communication in cloud model)
• Django web framework is used to implement Communication API
Python Web Application Framework – Django
• Django is an open source web application framework for developing web applications
in Python.
• A web application framework in general is a collection of solutions, packages and best
practices that allows development of web applications and dynamic websites.
• Django is based on the Model–Template–View architecture and provides separation
of the data model from the business rules and the user interface.
Python Web Application Framework – Django
• Django is based on the Model–Template–View architecture and provides separation
of the data model from the business rules and the user interface.
• Django provides a unified API to a database backend.
Python Web Application Framework – Django
• 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.
• Django consists of an object-relational mapper, a web templating system and a regular-
expression- based URL dispatcher.
Django Architecture
• Django uses a
• Model–Template–View (MTV) framework.
• 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:
Creating a Django Project and App:
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:
Most web applications have a database backend.
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.
Django supports various relational database engines including MySQL,
PostgreSQL, Oracle and SQLite3
Django supports non relational databases such as MongoDB. MongoDBcan be
added by installing additional engines
Django Framework :Starting Development with Django
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
Examples: Each class that represents
database table is a subclass of
django.db.models.model
1
2
https://github.com/Oskube/django-dht22/blob/master/dht22/models.py
https://thedig95.github.io/2018/09/08/data-api.html 3
To sync the model with the atabase,
run the following command:
when syncdb command is run for the first time,
https://readthedocs.org/projects/django-chartit/downloads/pdf/latest/ it creates all the tables defined in the Django
4
model in the configured database.
13
References: https://github.com/Oskube/django-dht22
https://riptutorial.com/django/example/12704/number-fields
Django Framework :Starting Development with Django
Django Admin site:
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
The Django admin site is enabled by adding django.contrib.admin and
django.contrib.admindocs to the INSTALLED_APPS section in the settings.py file
The admin site requires URL pattern definitions in the urls.py
To define the editable application models in the admin interface, a new file named admin.py
is to be created.
Enabling admin for Django Models
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:
Adding new items in the data table using 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.
Example of URL configuration:
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:
Dr Sumalatha Aradhya, Dept of CSE, SIT, 21
Tumakuru