0% found this document useful (0 votes)
4 views12 pages

Downloadfile

Django is an open-source web application framework for Python that follows the Model-Template-View architecture, allowing for the development of dynamic websites and applications. It provides a unified API for various database backends, supports both relational and non-relational databases, and includes features like an object-relational mapper and a built-in admin interface. Django's architecture separates data models, views, and templates, facilitating flexibility and ease of use in web development.

Uploaded by

mullasameera2011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

Downloadfile

Django is an open-source web application framework for Python that follows the Model-Template-View architecture, allowing for the development of dynamic websites and applications. It provides a unified API for various database backends, supports both relational and non-relational databases, and includes features like an object-relational mapper and a built-in admin interface. Django's architecture separates data models, views, and templates, facilitating flexibility and ease of use in web development.

Uploaded by

mullasameera2011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

FOUNDATIONS OF DATA ANALYTICS

10.3 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 allow development of web applications
and dynamic websites.

Django is based on the Model-Template-View architecture and provides a


separation of the data model from the business rules and the user interface.

R20 III-I SEM IV-UNIT Page 15


FOUNDATIONS OF DATA ANALYTICS

Django provides a unified API to a database backend. Thus, web applications built
with Django can work with different databases without requiring any code
changes.

Django consists of an object-relational mapper, a web templating system, and a


regular-expression-based URL dispatcher. Given the separation of the models,
views and templates, flexibility to use different databases, combined with the
powerful capabilities of the Python language and the Python ecosystem, Django is
one of the most suitable web application frameworks for big data analytics and
cloud applications.

10.3.1 Django Architecture:

Django adopts a Model-Template-View (MTV) architecture. The roles of model,


template and view are as follows:

Model:

The model acts as a definition of stored data and handles the interactions with the
database. A Django model is a Python class that outlines the variables and methods
for a particular type of data.

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
generates the web pages. View determines what data is to be displayed, retrieves

R20 III-I SEM IV-UNIT Page 16


FOUNDATIONS OF DATA ANALYTICS

the data from the database and passes the data to the template. For describing the
implementation of the model, template and view components of a Django
application we will use a reference application that maintains a record of
employees in a company.

Django comes with a built-in, lightweight Web server that can be used for
development purposes. When the Django development server is started the default
project can be viewed at the URL: http://localhost:8000.

Configuring a Database:

R20 III-I SEM IV-UNIT Page 17


FOUNDATIONS OF DATA ANALYTICS

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 backends thus
giving the freedom to choose the database. Django supports various relational
database engines including MySQL, PostgreSQL, Oracle and SQLite3. Support for
non-relational databases such as MongoDB can be added by installing additional
engines (e.g. Django-MongoDB engine for MongoDB).

Defining a Model

Model acts as a definition of the data in the database. The various database tables
for this application are defined as Classes in the Django model.

R20 III-I SEM IV-UNIT Page 18


FOUNDATIONS OF DATA ANALYTICS

Each class that represents a database table is a subclass of d


jango.db.models.Model class which contains all the functionality that allows the
models to interact with the database.

To sync the models with the database simply run the following command:
>python manage.py syncdb

Django Admin Site

Django provides an administration system that allows you to manage the project
without writing additional code. The admin system reads the Django model and
provides an interface that can be used to add content to the project. The Django
admin site is enabled by adding django.contrib.admin and d
jango.contrib.admindocs to the INSTALLED_APPS section in the settings.py
file. The admin site also requires URL pattern definitions in the urls.py file
described later in the URLs sections.

Figure 10.9 shows a screenshot of the Django admin interface. You can see all the
tables corresponding to the Django models in this screenshot.

R20 III-I SEM IV-UNIT Page 19


FOUNDATIONS OF DATA ANALYTICS

R20 III-I SEM IV-UNIT Page 20


FOUNDATIONS OF DATA ANALYTICS

Defining a View:

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. Conversely, the view also extracts the data posted in a
form in the template and inserts it into the database.

Typically, each page in a web application has a separate view, which is a Python
function in the views.py file. Views can also perform additional tasks such as
authentication, sending emails, etc.

Box 10.16 shows the source code for the Django views for the reference
application. The views correspond to the web pages that display the list of
employees, employee details, department details and project details.

In the views, the Django’s built in object-relational mapping API is used to retrieve
the data from the database tables. The object-relational mapping API allows the
developers to write generic code for interacting with the database without worrying
about the underlying database engine. So the same code for database interactions
works with different database backends.

In the views shown in Box 10.16, the table.ob jects.all query returns a QuerySet
with all the entries in a table. To retrieve specific entries, you can use table.ob
jects. filter(∗ ∗ kwargs) to filter out queries that match the specified condition.

R20 III-I SEM IV-UNIT Page 21


FOUNDATIONS OF DATA ANALYTICS

R20 III-I SEM IV-UNIT Page 22


FOUNDATIONS OF DATA ANALYTICS

Defining a Template :

A Django template is typically an HTML file (though it can be any text file such as
XML, email, CSS, JavaScript, CSV, etc.). Django templates allow separation of
the presentation of data from the actual data by using placeholders and associated
logic (using template tags). A template receives a context from the view and
presents the data in context variables in the placeholders.

The data is retrieved from the database in the view and passed to the template in
the form of a context dictionary. The for tags in the template loop over each item in
a sequence and the items are inserted with the placeholder tags (variable name
surrounded by braces, e.g. {{entry.email}}).

Template tag is any text that is surrounded by curly braces and percent signs (e.g.
{% for entry in student_entries %}). Django’s template language offers basic tags
such as f or, if , etc. and a number of built-in filters for modifying the output of
variables. Filters are attached to variables using a pipe character (|). For example
the filter join in {{entry.courses.all|join:", "}} joins a list with a string.

R20 III-I SEM IV-UNIT Page 23


FOUNDATIONS OF DATA ANALYTICS

R20 III-I SEM IV-UNIT Page 24


FOUNDATIONS OF DATA ANALYTICS

Defining the URL Patterns

URL Patterns are a way of mapping the URLs to the views that should handle the
URL requests. The URLs requested by the user are matched with the URL patterns
and the view corresponding to the pattern that matrices the URL is used to handle
the request. Box 10.21 shows an example of the URL patterns for the reference
application. As seen in this example, the URL patterns are constructed using
regular expressions. The simplest regular expression (r‘∧ $’) corresponds to the
root of the website or the home page. More complex URLs allow capturing values.
R20 III-I SEM IV-UNIT Page 25
FOUNDATIONS OF DATA ANALYTICS

For example the pattern: url(r‘∧ employee/(?P\w+)’,


‘myapp.views.employeedetail’) captures the employee number from the URL to
the variable query and passes it to the employeedetail view.

R20 III-I SEM IV-UNIT Page 26

You might also like