FSD-Module 1
FSD-Module 1
Database: Database is a collection of inter related data which helps in efficient retrieval, insertion
and deletion of data from database and organizes the data in the form of tables, views, schemas,
reports etc.
Oracle: Oracle database is the collection of data which is treated as a unit. The purpose of this
database is to store and retrieve information related to the query. It is a database server and used
to manage information.
Mongo DB: Most popular NoSQL database is an open source document-oriented database. The
term NoSQL means non-relational. MongoDB is not based on the table like relational database
structure but provides an altogether different mechanism for storage and retrieval of data.
SQL: Structured query language is a standard database language which is used to create, maintain
and retrieve the relational database.
Popular Stacks:
MEAN stack- MongoDB, Express, Angular JS, Node.js
MERN stack- MongoDB, Express, React JS, Node.js
Django stack- Django, Python and MySQL as database
LAMP stack- Linux, Apache, MySQL, PHP
The outer mysite/ is a root directory is just a container for the project. Its name doesn’t matter to
Django; can rename it to anything.
manage.py: A command-line utility that lets to interact with this Django project in various
ways. Use manage.py to start the project webserver, migrate the database, and other
interactions.
The inner mysite/ directory is the actual Python package for the project. Its name is the
Python package name so need to use to import anything inside it (e.g. mysite.urls).
mysite/_init_.py: An empty file that tells Python that this directory should be considered a
Python package.
mysite/settings.py: Settings/configuration for the Django project. This file contains
information about the database connection, time zones, included packages, etc.
mysite/urls.py: The URL declarations for this Django project, a “table of contents” of the
Django-powered site.
mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve the project.
(WSGI-Web Server Gateway Interface)
How the Django architecture work:
It follows the Model-View-Template (MVT) architecture pattern.
Models- describe the data structure and database scheme
Views- retrieve data from the model, perform calculations on the data and contains what people
can see.
Template- control how data is presented to someone, including which Views to display and how
to format them.
Why Django?
1. It has extensive code libraries and enables developers to reuse chunks of code instead of
writing an entire application from scratch.
2. It comes with a number of in-built functions such as REST framework for creating APIs
integrate with various database and does not require any third party extension to operate.
3. The range of Django web applications include MVPs and prototypes, content management
systems, e-commerce, e-learning, mHealthplatform, collaboration software, online
marketplace.
4. Django is written in python and provides a set of tools and libraries to help you built web
applications quickly and efficiency.
5. Simplicity, Security, excellent documentation, extensive code libraries and the support of
fast-paced development.
As of today Django apps are extensively leveraged for projects involving AI, Machine
Learning and BigData.
Django is primarily used for back end development. It can be used for front end development
despite it is not its main focus.
Web framework:
A web framework or web development framework is a set of resources and tools for
software developers to build and manage web applications, web services and websites as well as
to develop Application Programming Interfaces (APIs). It provides a standard way to build and
deploy web applications on the World Wide Web. It provides libraries for database access,
templating frameworks and session management and they often promote code reuse. It also target
development of dynamic website they are also applicable to static websites.
Web application frameworks software:
Django
Flask
ASP.NET
Express.js
Ruby on Rails
Angular
jQuery
Bootstrap
CGI Application:
Example: CGI script written in python to displays the ten most recently published books from a
database.
#!/usr/bin/python
(Specify exactly which interpreter will be used to run the script on a particular system. This is a
hardcoded path to the python interpreter for that particular system. The advantage of this is that it
can use a specific python version to run the code).
import MySQLdb
(MySQLdb is an interface for connecting to a MySQL database server from python. Using
python’s MySQLdb module for connecting to the database which is at any sever that provide
remote access).
(Python module names are case sensitive, even on case–insensitive file systems. Therefore
MySQLdb and mysqldb are two different modules/packages. Use import MySQLdb).
Taken together, these pieces loosely follow the Model-View-Controller (MVC) design
pattern. Simply put, MVC defines a way of developing software so that the code for defining and
accessing data (the Model) is separate from request routing logic (the Controller), which in turn is
separate from the user interface (the View).
A key advantage of this approach is that components are loosely coupled. That is, each
distinct piece of a Django-powered Web application has a single key purpose and can be
changed independently without affecting the other pieces.
For example,
A developer can change the URL for a given part of the application without affecting the
underlying implementation.
A designer can change a page’s HTML without having to touch the Python code that renders it.
A database administrator can rename a database table and specify the change in a single place,
rather than having to search and replace through a dozen files.
Django’s History:
It helps to understand why the framework was created.
1. Write a Web application from scratch.
2. Write another Web application from scratch.
3. Realize the application from step 1 shares much in common with the application
from step 2.
4. Refactor the code so that application 1 shares code with application 2.
5. Repeat steps 2–4 several times.
6. Realize that the framework is invented.
This is precisely how Django itself was created!
Django grew organically from real-world applications written by a Web development team in
Lawrence, Kansas. It was born in the fall of 2003, when the Web programmers at the Lawrence
Journal-World newspaper, Adrian Holovaty and Simon Willison, began using Python to build
applications.
In summer 2005, after having developed this framework to a point where it was efficiently
powering most of World Online’s sites, the World Online team, which now included Jacob
Kaplan-Moss, decided to release the framework as open source software. They released it in July
2005 and named it Django, after the jazz guitarist Django Reinhardt.
When an URL is visited, the view function will receive a request. It will query the database
defined in models.py and done all the necessary processing and render the response using template
files.
Django’s view actually acts as the controller function in typical MVC architecture
Templates have a similar role as view in MVC.
Model: This is the abstraction layer for structuring and manipulating the data of the web
application. It acts as an interface for maintaining data. This is the logical data structure behind
the entire application and helps to handle the database.
View: This layer encapsulates the logic responsible for processing a user request and returns a
response. It is a user interface to execute the logic and interact with the models. It is responsible
for displaying all or a portion of data to the user.
Django view function is a special type of python function that accepts a web request as input and
output a particular web response. It takes what is in the Django template file. Usually from HTML,
CSS, Javascript and render a webpage. Example of Django view response include HTML web
page contents, an image, and an XML document, anything that a web browser can render.
Template: provides a designer friendly. Syntax for rendering the information to be presented to
the user. It contains the static parts of the desired HTML output along with some special syntax,
also known as Django Template Language (DTL), describing how dynamic content will be
inserted.
MVC vs MVT:
Web application frameworks uses MVC architecture. MVC is popular as it isolates the
application logic from the user interface layer and different parts of the application can be
developed separately. Here the controller part is the software code that controls the interaction
between the Model and the View.
Django works on the concepts of MVT, it itself takes care of the controller part and no
need to worry much about how the data moves between the Model and the View.
• imports all objects from the django.conf.urls.defaults module, including a function called
patterns.
• calls the function patterns() and the result is saved into a variable called urlpatterns. The
patterns() function gets passed only a single argument—the empty string. This string is used to
supply a common prefix for View functions. The variable defines the mapping between URLs and
the code that handles those URLs.
If the URLconf is empty, Django assumes that it just started a new project and hence
displays the message.
Variable urlpatterns which Django expectes to find in the ROOT_URLCONF module.
By default everything in the URLCONF is commented out, the Django application is a
blank state.
Python path:
Python path is the list of directories on the system where Python looks when using the
Python import statement.
Python path,
['', '/usr/lib/python2.4/site-packages', '/home/username/djcode/'].
While executing the Python code from foo import bar, Python will first check for a module
called foo.py in the current directory.
The first entry in the Python path, an empty string, means “the current directory”. If that
file doesn’t exist, Python will look for the file /usr/lib/python2.4/site-packages/foo.py. If that file
doesn’t exist, it will try /home/username/djcode/foo.py. Finally, if that file doesn’t exist, it will
raise ImportError.
To see the value of the Python path, start the Python interactive interpreter and type import
sys, followed by print sys.path.
But no need to worry about setting the Python path—Python and Django will take care of
things automatically behind the scenes.
manage.py file does this.
The figure shows the complete flow of Django request and response
When an HTTP request comes from the browser, a server-specific handler
constructs the HttpRequest passed to later components and handles the flow of the
response processing.
Handler calls Request or View middleware. This middleware are useful for
augmenting incoming HttpRequest objects as well as providing special handling
for specific types of requests. It returns either a HttpResponse or processing
bypasses the view.
Exception middleware is take care of the bugs.
If the view function raises an exception, control passes to the exception
middleware. If this middleware does not return an HttpResponse, the exception is
reraised.
But Django includes default views that create a friendly 404 and 500 response.
Before sending to browser the response middleware postprocessing an
HttpResponse or cleanup of request-specific resources.
Second View:
Dynamic URLs:
In dynamic Web applications, URL contains parameters that influence the output of the page.
Example:
Create a view that displays the current date and time offset by a certain number of hours.
/time/plus/1/- displays the date/time one hour into the future.
urlpatterns = patterns('',
(r'^time/$', current_datetime),
(r'^time/plus/1/$', one_hour_ahead),
(r'^time/plus/3/$', three_hours_ahead),
(r'^time/plus/4//$', four_hours_ahead),
)
To overcome the problem do some abstraction here.
Wildcard URL patterns
URL pattern is a regular expression. Use the regular expression pattern \d+ to match one
or more digits:
from django.conf.urls.defaults import *
from mysite.views import current_datetime, hours_ahead
urlpatterns = patterns('',
(r'^time/$', current_datetime),
(r'^time/plus/\d+/$', hours_ahead),
)
(r'^time/plus/\d{1,2}/$', hours_ahead),
Use single View function for any arbitrary hour offset. Do this by placing parentheses
around the data in the URL pattern which is want to save?
(r'^time/plus/(\d{1,2})/$', hours_ahead),