0% found this document useful (0 votes)
6 views18 pages

Django

Uploaded by

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

Django

Uploaded by

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

What is an application?

An application, also referred to as an application program or application


software, is a computer software package that performs a specific function
directly for an end user or, in some cases, for another application. An
application can be self-contained or a group of programs. The program is a set
of operations that runs the application for the user.

Types of applications:

• Standalone applications:
standalone applications are a type of software program that is designed to run
on a single computer or local machine of the user, without the need for a server
or internet connection. This application provides a number of benefits, such as
offline capabilities and greater customization.

• Web based applications:


A web-based application is any program that is accessed over a network
connection using HTTP, rather than existing within a device’s memory. Web-
based applications often run inside a web browser. However, web-based
applications also may be client-based, where a small part of the program is
downloaded to a user’s desktop, but processing is done over the internet on an
external server.

• Client – server based applications:


a client/server application consists of a client program that consumes services
provided by a server program. The client requests services from the server by
calling functions in the server application. In a distributed computing
environment, where the client program and the server program execute on
different machines and possibly even on different platforms, the client and
server communicate through a communications layer that is often
called middleware.

• Mobile applications:
A mobile application, most commonly referred to as an app, is a type of
application software designed to run on a mobile device, such as a smartphone
or tablet computer. Mobile applications frequently serve to provide users with
similar services to those accessed on PCs. Apps are generally small, individual
software units with limited function.
Architectures of Software development:

2 tier architecture:

3 tier architecture:
While working in 3 tier architecture there will be 3 layers to develop an
application.

• Presentation layer:
It represents user interface which is considered as frontend.
We can develop Front-End content by using the following technologies:
HTML, JS, CSS, jQuery and Bootstrap
jQuery and Bootstrap are advanced front-end technologies, which are
developed by
using HTML, CSS and JavaScript only.
HTML:
Hypertext Mark-up Language
Every web application should contain HTML. i.e. HTML is the mandatory
technology for
web development. it represents structure of web page
CSS: Cascading Style Sheets
It is optional technology, still every web application contains CSS.
The main objective of CSS is to add styles to the HTML Pages like colours,
fonts, borders
etc.
Java Script:
It allows to add interactivity to the web application including programming
logic.
The main objective of Java Script is to add functionality to the HTML Pages. ie
to add
dynamic nature to the HTML Pages.
HTML Meant for Static Responses
HTML+JS Meant for Dynamic Responses

• Application layer:
It represents the server layer or logical layer where actual code is present and
where data get processed. It is also considered as backend.
Backend is responsible to generate required response to the end user, which is
displayed by the Front-End.
Back-End has 2 important components:
1) The Language like Java, Python etc
2) The Frameworks like Django, Pyramid, Flask etc

• Data base layer:


Data base is where actual data get stored in tabular structure.
We can process CRUD operation on this stored data by using different query
languages.
The Database like SQLite, Oracle, MySQL etc
frameworks:

Python frameworks automate the implementation of several tasks and give


developers a structure for application development. Each framework comes
with its own collection of modules or packages that significantly reduce
development time. A Python framework can either be full-stack, micro, or
asynchronous.

Why framework:

There are a number of redundant operations in web development. Python


frameworks allow developers to reuse code for common HTTP operations. The
projects are structured in such a way that other developers with knowledge of
that particular framework can easily use it to build and maintain web
applications.

Uses of framework:

 Easier implementation
 Good documentation
 Efficient operations
 Secure framework
 Open-source
 Code reusability
 Easy integration

Library VS Framework:

A library is a collection of packages that perform specific operations whereas a


framework contains the basic flow and architecture of an application. The major
difference between them is the complexity. Libraries contain a number of
methods that a developer can just call whenever they write code. With
frameworks, you would need to build the rest of the other functionalities.
However, frameworks offer a key advantage - flexibility. They provide the
necessary tools and features that developers can extend and build upon.
Different Frameworks of Python:

 Django
 Web2py
 Flask
 Bottle
 Cherrypy
 Frappe
 Grok
 FastAPI
 zope

web framework:

web application frameworks or ‘web frameworks’ as “a software


framework that is designed to support the development of web applications
including web services, web resources and web APIs”. In simple words, web
frameworks are a piece of software that offers a way to create and run web
applications. Thus, you don’t need to code on your own and look for probable
miscalculations and faults.

As web standards began to advance, the app logic shifted towards the
client- ensuring smarter communication between the user and the web
application. With logic on client-side, they (client) can react swiftly to user
input. This makes web apps more responsive, easily navigate-able on any device.
Thus, we have two functions of frameworks — a) the one to work on the server
side, that helps to set up app logic on the server (backend) or b) to work on the
client-side (front end).

1. Server-side Frameworks:

The rules and architecture of the server-side frameworks permit you to create
simple pages, landings and forms of different kinds. To create a web application
with a well-developed interface you need a wider range of functionality. Server-
side frameworks handle HTTP requests, database control and management, URL
mapping, etc. These frameworks can improve security and form the output data-
simplifying the development process. Some of the top server-side frameworks
are –
 .NET (C#)
 Django (Python)
 Ruby on Rails (Ruby)
 Express (JavaScript/Node.JS)
 Symfony (PHP)

2. Client-side Frameworks
Client-side frameworks don’t take care of the business logic like the server-side
ones. They function inside the browser. Therefore, you can enhance and
implement new user interfaces. A number of animated features can be created
with frontend and single page applications. Every client-side framework varies
in functionality and use. Here are some client-side frameworks for comparison’s
sake; all of whom use JavaScript as their programming language-
 Angular
 Ember.JS
 Vue.JS
 React.JS

Web application framework- Architecture:


MVC (Model-View-Controller) architecture:
The reason why this pattern is preferred lies in its rational design that separates
the app logic from the interface and forms the three essential parts that are
represented in the architecture’s name — MVC (Model-View-Controller).
Model
The Model comprises of all the data, business logic layers, its guidelines and
functions. The Model, upon getting user input data from the Controller, tells the
way an updated interface should be displayed directly to the View.

View
The View is for the graphical representation of the data like graph or charts etc.
It is the apps’ front-end. The View gets the user input and communicates the
same to the Controller for examination and then update and reconstructs itself
according to the Model’s instructions, or the Controller’s in case the
modification requirement is minimum.

Controller
The Controller translates the input data into the scope of commands of the
previous ones. It is the midway between the Model and the View. It gets the user
input from the View; after processing it, the Controller notifies the Model (or
View) of the changes required.

The talking point about many discussions regarding the Controller is that it isn’t
always essential (giving more importance to the separation of logic from the
interface). Assigning input processing, however, to either Model or View messes
up the MVC’s traditional mantra of ‘Separated Presentation’-where tasks are
separated on type-basis. For the project to remain transparent, adaptable and
maintainable, each component in the MVC must be responsible for a sole line of
tasks.

MVT(Model View Template):


Model
The data you want to present, usually data from a database.
The model provides data from the database.
In Django, the data is delivered as an Object Relational Mapping (ORM), which
is a technique designed to make it easier to work with databases.
The most common way to extract data from a database is SQL. One problem
with SQL is that you have to have a pretty good understanding of the database
structure to be able to work with it.
Django, with ORM, makes it easier to communicate with the database, without
having to write complex SQL statements.
The models are usually located in a file called models.py.

View
A view is a function or method that takes http requests as arguments, imports
the relevant model(s), and finds out what data to send to the template, and
returns the final result.
A request handler that returns the relevant template and content - based on the
request from the user.
The views are usually located in a file called views.py.

Template
A template is a file where you describe how the result should be represented.
A text file (like an HTML file) containing the layout of the web page, with logic
on how to display the data.
Templates are often .html files, with HTML code describing the layout of a web
page, but it can also be in other file formats to present other results, but we will
concentrate on .html files.
Django uses standard HTML to describe the layout, but uses Django tags to add
logic:
Ex:
<h1>My Homepage</h1>
<p>My name is {{ firstname }}.</p>
The templates of an application is located in a folder named templates.

URLs:
Django also provides a way to navigate around the different pages in a website.
When a user requests a URL, Django decides which view it will send it to.
This is done in a file called urls.py.
Django:
• Django is the most popular web application framework for Python.
• Django is a back-end server-side web framework.
• Django is a free and open-source web framework written in python.
• It follows the Model-View-Template (MVT) architectural pattern.
• It is maintained by the Django Software Foundation (DSF).
• Django makes it easier to build web pages using Python.
• Django is a Python framework that makes it easier to create web sites
using Python.
• Django takes care of the difficult stuff so that you can concentrate on
building your web applications.
• Django emphasizes reusability of components, also referred to as DRY
(Don't Repeat Yourself), and comes with ready-to-use features like login
system, database connection and CRUD operations (Create Read Update
Delete).
• Django is especially helpful for database driven websites.
• The following are various popular web applications which are developed
by using Python and Django. Are. YouTube, Dropbox, Quora,
Instagram, Reddit, Yahoo Maps etc.

Django History:

Django was invented by Lawrence Journal-World in 2003, to meet the short


deadlines in the newspaper and at the same time meeting the demands of
experienced web developers.
Initial release to the public was in July 2005.

Features of Django Framework:

1) Fast:
Django was designed to help developers take applications from concept to
completion
as quickly as possible.
2) Fully loaded:
Django includes a greater number of inbuilt modules, we can use to common
Web development
tasks. Django takes care of user authentication, content administration,...
3) Security:
Django takes security seriously and helps developers avoid many common
security
mistakes, such as hacking data. Its user authentication system provides a secure
way to manage user accounts and passwords.
4) Scalability:
Busy sites in world use Django’s ability to quickly and flexibly scale
to meet the heaviest traffic demands.
5) Versatile:
Companies, organizations and governments have used Django to build all sorts
of things from content management systems to social networks to scientific
computing platforms.

Working with Python-Django:

Environments:
There are 2 types of environments we need to deal with, when we are working
on Django projects.
System environment:
it means everything the system interacts with. For a program running on a single
computer, the system environment definition might include any other programs
running on the computer, the operating system, all the computer's configuration
settings, and the computer's physical characteristics. In simple words it’s a
common or global environment shared by all applications installed in a system
for processing

Virtual environment:
A virtual environment is a networked application that allows a user to interact
with both the computing environment and the work of other users.
Creating Project in System Environment Directly:
• Refer all the commands of Django from Django documentation for
better performance.
• Create a project of Django:
• Create a folder
• Open terminal
• Run cmd pip install django
• Run cmd django-admin startproject projectname
• After creating a project enter into the project folder
• You will see a manage.py file and same named folder as of project
name.
• Run cd projectname
• Run python manage.py runserver
• You will see a ip address in terminal
• Click on that address and check server get activated..

Creating Project in Virtual Environment:

• Install python idle


Run python –version in cmd
If you get version of python then its successfully installed
• Check pip version
Run pip –version in cmd
• Install v s code
• Create a folder in any drive (preferably not C drive)
• Open the folder in VS code
• Creating virtual Environment
It is suggested to have a dedicated virtual environment for each
Django project, and one way to manage a virtual environment
is venv, which is included in Python.
The name of the virtual environment can be anything.
Run command python -m venv vitual_env_name
This will set up a virtual environment, and create a folder named "
vitual_env_name " with subfolders and files, like this:
vitual_env_name
Include
Lib
Scripts
pyvenv.cfg
Then you have to activate the environment, by typing this
command:
cd vitual_env_name\Scripts
activate
Note: You must activate the virtual environment every time you
open the command prompt to work on your project.
• Install Django in virtual environment
Run cmd pip install Django
Check the version of Django by using cmd django-admin --version
• Create a project
A Django project is a collection of applications and configurations
which forms a full web
application. Eg: Bank Project
Run cmd to create project django-admin startproject projectname
Django creates a project folder on computer, with this content:
Project_name
manage.py
project_name/
Contents in project_name/ folder are as below:
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
manage.py is a python program to which we give an argument
now your project is ready to run
Navigate to the /project_name folder by using cd
run the cmd to execute project python manage.py runserver
after execution you will get ip address like below:
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Open a new browser window and type 127.0.0.1:8000 in the
address bar.

The result:

• Create an application:
A Dango Application is responsible to perform a particular task in our
entire web
application. Eg: loan app, registration app
If the server is still running, and you are not able to write
commands, press [CTRL] [BREAK], or [CTRL] [C] to stop the
server and you should be back in the virtual environment.
Run the cmd to create an application
py manage.py startapp app_name
Django creates a folder named app_name in project folder, with
this content:
Project_name
manage.py
Project_name /
app_name/
migrations/
__init__.py
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
How does Django Work?
Django follows the MVT design pattern (Model View Template).
We already discussed MVT structure. Now let’s see them in brief.

Views
Django views are Python functions that takes http requests and returns http
response, like HTML documents.
A web page that uses Django is full of views with different tasks and missions.
Views are usually put in a file called views.py located on your app's folder.

# Create your views here.


Find it and open it:
Navigate to project/app/views.py:
from django.shortcuts import render
from django.http import HttpResponse

#creating requests and sending back resonses:


def view_name(request):
return HttpResponse("Hello world!")

Note: The name of the view does not have to be the same as the application.
To execute the view, we must call the view via a URL.

URLs:
Django also provides a way to navigate around the different pages in a website.
When a user requests a URL, Django decides which view it will send it to.
This is done in a file called urls.py.
Urls are meant to provide paths. We can use url file which is already present in
project folder or we can create a file named urls.py in the same folder as
the views.py file, and type this code in it:
project/app/urls.py
from django.urls import path
from . import views

urlpatterns = [
path('view_name/', views.view_name, name='view_name'),
]

The urls.py file you just created is specific for the current application. We have
to do some routing in the root directory project as well. This may seem
complicated, but for now, just follow the instructions below.
There is a file called urls.py on the project folder, open that file and add
the include module in the import statement, and also add a path() function in
the urlpatterns[] list, with arguments that will route users that comes in
via 127.0.0.1:8000/.
Then your file will look like this:
project/project/urls.py
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
path('', include('app_name.urls')),
path('admin/', admin.site.urls),
]

Note:
If the server is not running, navigate to the /project_name folder and
execute this command in the command prompt:
py manage.py runserver
In the browser window, type 127.0.0.1:8000/members/ in the address bar.
When you have installed Django and created your first Django web application,
and the browser requests the URL, this is basically what happens:

Django receives the URL, checks the urls.py file, and calls the view that
matches the URL.
The view, located in views.py, checks for relevant models.
The models are imported from the models.py file.
The view then sends the data to a specified template in the template folder.
The template contains HTML and Django tags, and with the data it returns
finished HTML content back to the browser.
Templates:
Create a templates folder inside the members folder, and create a HTML
file named myfirst.html.
The file structure should be like this:
project
manage.py
project/
app/
templates/
myfirst.html

Open the HTML file and insert the following sample html content:
project/app/templates/myfirst.html:
<!DOCTYPE html>
<html>
<body>

<h1>Hello World!</h1>
<p>Welcome to my first Django project!</p>

</body>
</html>

Modify the View:


Open the views.py file and replace the members view with this:
project/app /views.py:
from django.http import HttpResponse
from django.template import loader

def view_name(request):
template = loader.get_template('myfirst.html')
return HttpResponse(template.render())

Change Settings
To be able to work with more complicated stuff, We have to tell Django
that a new app is created.
This is done in the settings.py file in the project folder.
Look up the INSTALLED_APPS[] list and add the app like this:
my_tennis_club/my_tennis_club/settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name'
]

Then run this command:


python manage.py migrate
python manage.py runserver

In the browser window, type 127.0.0.1:8000/members/ in the address bar.


The result should look like this:

You might also like