Downloadfile
Downloadfile
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.
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
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:
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.
To sync the models with the database simply run the following command:
>python manage.py syncdb
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.
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.
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.
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