SlideShare a Scribd company logo
6
Most read
9
Most read
12
Most read
Python Web Framework
Balakumar Parameshwaran
24-10-2013

http://www.balakumarp.com
Agenda
 Introduction
 Features
 Installation
 Django Architecture
 Project structure
 Settings

 Project / Site creation
 URL Dispatcher
 Who uses it?

 Questions

2
Introduction
Django is a free and open source web application framework, written in
Python, which follows the Model–View–Controller architectural pattern.
It is maintained by the Django Software Foundation (DSF), an
independent organization.
 Encourages rapid development and clean, pragmatic design.

 Named after famous Guitarist Django Reinhardt
 Developed by Adrian Holovaty & Jacob Kaplan-moss
 Created in 2003, open sourced in 2005
 1.0 Version released in Sep 3 2008, now 1.5.4

3
Features


Object Relational Mapper - ORM



MVC (MVT) Architecture



Focuses on automating as much as possible and adhering to the DRY principle



Template System



Out of the box customizable Admin Interface, makes CRUD easy



Built-in light weight Web Server



Elegant URL design



Custom Middleware



Authentication / Authorization



Internationalization support



Cache framework, with multiple cache mechanisms



Fast Development



Free, and Great Documentation
4
Installation
Prerequisites



Python
PIP for installing Python packages (http://www.pip-installer.org/en/latest/installing.html)

 pip install Django==1.5.4
o

OR https://www.djangoproject.com/download/ - python setup.py install

 pip install mysql-python
o

MySQL on windows https://pypi.python.org/pypi/MySQL-python/1.2.4

 Add Python and Django to env path
o

PYTHONPATH D:Python27

o

Path D:Python27; D:Python27Libsite-packages; D:Python27Libsite-packagesdjangobin;

 Testing installation
o

shell> import django; django.VERSION;

5
Django Architecture
Models
Views
Templates
Controller

Describes your data
Controls what users sees
How user sees it
URL dispatcher

6
Project Directory Structure
demosite/ ---------------------------------- Just a container for your project. Its name doesn’t

matter to Django; you can rename

it to anything you like.

manage.py ------------------------- A command-line utility that lets you interact with this Django project in various ways.
Type python manage.py help. You should never have to edit this file.

demosite/ ------------------------- Actual Python package for your project. Use this name to import anything inside it
(e.g. import demosite.settings)

__init__.py ----------------- A file required for Python to treat the demosite directory as a package.
settings.py ----------------- Settings/configuration for this Django project
urls.py ---------------------- Root URL config, the URLs for this Django project, provides mapping to views
wsgi.py ---------------------- An entry-point for WSGI-compatible webservers to serve your project
templates/ ----------------- HTML files , renders based on views. You can change to any dir, configurable in settings.py
static/ ----------------------- CSS, JS, images.. etc, configurable in settings.py
demoapp/ ----------------__init__.py -------urls.py -----------views.py ---------- Responsible for processing a user’s request and for returning the response
models.py --------- A model is the single, definitive source of information about your data.
Generally, each model maps to a single database table.

admin.py ---------- It reads metadata in your model to provide a powerful and production-ready interface
forms.py ----------- To create and manipulate form data
7
Settings


Project settings.py overrides from <python>/Lib/site-packgaes/django/conf/global_settings.py



Set DJANGO_SETTINGS_MODULE for your Project, tells django which settings to be used. (demoproject.settings)


export/set DJANGO_SETTINGS_MODULE=demoproject.settings



For server mod_wsgi: os.environ['DJANGO_SETTINGS_MODULE'] = 'demoproject.settings'



DEBUG

True or False



DATABASES ENGINE

postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.. etc



ROOT_URLCONF



MEDIA_ROOT

directory that will hold user-uploaded files



MEDIA_URL

To serve media files



STATIC_ROOT

To any server static files css, js.. and admin UI files (can add more dirs to STATICFILES_DIRS)



STATIC_URL

To serve static files



TEMPLATE_DIRS

Template directories

Using settings in Python code
from django.conf import settings
if settings.DEBUG:
# Do something
8
Project / Site Creation
 Creating new Project


django-admin.py startproject demoproject

A project is a collection of applications

 Creating new Application


python manage.py startapp demosite

An application tries to provide a single, relatively self-contained set of related functions

 Using the built-in web server


python manage.py runserver



python manage.py runserver 80

Runs by default at port 8000
It checks for any error and validate the models. Throws errors/warnings for any misconfigurations and invalid
entries.

9
URL Dispatcher / Patterns


Root URL should be configured in settings.py
o



ROOT_URLCONF = 'app.urls'

Syntax
patterns(prefix,
(regular expression, Python callback function [, optional dictionary [, optional name]])
)
Example:
urlpatterns = patterns(' ',
(r'^articles-year/$', 'mysite.news.views.articles_year'),
)
Note:
o No need to add a leading slash (/articles-year)
o The 'r' in front of each regular expression string is optional but recommended. It tells Python that a
string is "raw" -- that nothing in the string should be escaped.
In python, the ‘’ backslash character in control chars must be escaped for regular expression use.
Basically we have to add one more slash i.e t, b. To work around backslash plague, you can raw string,
by prefixing the string with the letter r.
10


Can include other URLconf modules
urlpatterns = patterns(' ',
url(r'^support/', include('demoproject.support.urls')),
)



Using Prefix
urlpatterns = patterns(' ',
(r'^articles/(d{4})/$', 'mysite.news.views.articles_year'),
(r'^articles/(d{4})/(d{2})/$', 'mysite.news.views.articles_month'),

)
Here mysite.news.views is common, so can be rewritten as follows
urlpatterns = patterns('mysite.news.views',
(r'^articles/(d{4})/$', 'articles_year'),
(r'^articles/(d{4})/(d{2})/$', 'articles_month'),

)



Passing extra arguments and Dictionary mapping
patterns(' ',
(r'^articles/(?P<year>d{4})/$', 'articles_year'), {'foo': 'bar'}),
)
We can get the values in views.py as year='2005', foo='bar'

11
Who uses Django?
 BitBucket

 DISQUS (serving 400 million people)
 Pinterest
 Instagram
 dPaste
 Mozilla (support.mozill, addons.mozilla)
 NASA
 PBS (Public Broadcasting Service)

 The Washington Post, NY Times, LA Times, The Guardian
 National Geographic, Discovery Channel

12

More Related Content

What's hot (20)

ODP
Django for Beginners
Jason Davies
 
KEY
Introduction to Django
James Casey
 
PDF
Introduction to django
Ilian Iliev
 
PDF
Django Introduction & Tutorial
之宇 趙
 
PPT
DJango
Sunil OS
 
PDF
Introduction to django framework
Knoldus Inc.
 
PPTX
Python/Flask Presentation
Parag Mujumdar
 
PPTX
Django Interview Questions and Answers
Python Devloper
 
PPTX
Flask – Python
Max Claus Nunes
 
PDF
Quick flask an intro to flask
juzten
 
PPTX
Web development with django - Basics Presentation
Shrinath Shenoy
 
PDF
Free django
Eugen Oskin
 
KEY
Introduction Django
Wade Austin
 
PDF
Django
Amanpreet Singh
 
PDF
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Edureka!
 
PDF
Django Tutorial | Django Web Development With Python | Django Training and Ce...
Edureka!
 
PPTX
Angular modules in depth
Christoffer Noring
 
PDF
Building an API with Django and Django REST Framework
Christopher Foresman
 
PPTX
Introduction to Django Rest Framework
bangaloredjangousergroup
 
PPTX
Django Architecture Introduction
Haiqi Chen
 
Django for Beginners
Jason Davies
 
Introduction to Django
James Casey
 
Introduction to django
Ilian Iliev
 
Django Introduction & Tutorial
之宇 趙
 
DJango
Sunil OS
 
Introduction to django framework
Knoldus Inc.
 
Python/Flask Presentation
Parag Mujumdar
 
Django Interview Questions and Answers
Python Devloper
 
Flask – Python
Max Claus Nunes
 
Quick flask an intro to flask
juzten
 
Web development with django - Basics Presentation
Shrinath Shenoy
 
Free django
Eugen Oskin
 
Introduction Django
Wade Austin
 
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Edureka!
 
Django Tutorial | Django Web Development With Python | Django Training and Ce...
Edureka!
 
Angular modules in depth
Christoffer Noring
 
Building an API with Django and Django REST Framework
Christopher Foresman
 
Introduction to Django Rest Framework
bangaloredjangousergroup
 
Django Architecture Introduction
Haiqi Chen
 

Similar to Django - Python MVC Framework (20)

PPTX
Django framework
TIB Academy
 
DOCX
Akash rajguru project report sem v
Akash Rajguru
 
ODP
Web Development in Django
Lakshman Prasad
 
PPTX
Django
Harmeet Lamba
 
PDF
بررسی چارچوب جنگو
railsbootcamp
 
PPTX
How to lock a Python in a cage? Managing Python environment inside an R project
WLOG Solutions
 
PPTX
Django
Abhijeet Shekhar
 
ODP
Developing Drizzle Replication Plugins
Padraig O'Sullivan
 
PDF
Continuous Deployment @ AWS Re:Invent
John Schneider
 
PDF
OpenERP Technical Memento V0.7.3
Borni DHIFI
 
DOCX
Company Visitor Management System Report.docx
fantabulous2024
 
PPT
Mini Curso Django Ii Congresso Academico Ces
Leonardo Fernandes
 
PDF
EuroPython 2013 - Python3 TurboGears Training
Alessandro Molina
 
PDF
Rest api with Python
Santosh Ghimire
 
PDF
Instrumenting plugins for Performance Schema
Mark Leith
 
PDF
Let's build Developer Portal with Backstage
Opsta
 
PDF
Open Source RAD with OpenERP 7.0
Quang Ngoc
 
PDF
Rediscovering Spring with Spring Boot(1)
Gunith Devasurendra
 
PDF
Heroku pycon
Sabato Severino
 
Django framework
TIB Academy
 
Akash rajguru project report sem v
Akash Rajguru
 
Web Development in Django
Lakshman Prasad
 
بررسی چارچوب جنگو
railsbootcamp
 
How to lock a Python in a cage? Managing Python environment inside an R project
WLOG Solutions
 
Developing Drizzle Replication Plugins
Padraig O'Sullivan
 
Continuous Deployment @ AWS Re:Invent
John Schneider
 
OpenERP Technical Memento V0.7.3
Borni DHIFI
 
Company Visitor Management System Report.docx
fantabulous2024
 
Mini Curso Django Ii Congresso Academico Ces
Leonardo Fernandes
 
EuroPython 2013 - Python3 TurboGears Training
Alessandro Molina
 
Rest api with Python
Santosh Ghimire
 
Instrumenting plugins for Performance Schema
Mark Leith
 
Let's build Developer Portal with Backstage
Opsta
 
Open Source RAD with OpenERP 7.0
Quang Ngoc
 
Rediscovering Spring with Spring Boot(1)
Gunith Devasurendra
 
Heroku pycon
Sabato Severino
 
Ad

Recently uploaded (20)

PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
July Patch Tuesday
Ivanti
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Ad

Django - Python MVC Framework

  • 1. Python Web Framework Balakumar Parameshwaran 24-10-2013 http://www.balakumarp.com
  • 2. Agenda  Introduction  Features  Installation  Django Architecture  Project structure  Settings  Project / Site creation  URL Dispatcher  Who uses it?  Questions 2
  • 3. Introduction Django is a free and open source web application framework, written in Python, which follows the Model–View–Controller architectural pattern. It is maintained by the Django Software Foundation (DSF), an independent organization.  Encourages rapid development and clean, pragmatic design.  Named after famous Guitarist Django Reinhardt  Developed by Adrian Holovaty & Jacob Kaplan-moss  Created in 2003, open sourced in 2005  1.0 Version released in Sep 3 2008, now 1.5.4 3
  • 4. Features  Object Relational Mapper - ORM  MVC (MVT) Architecture  Focuses on automating as much as possible and adhering to the DRY principle  Template System  Out of the box customizable Admin Interface, makes CRUD easy  Built-in light weight Web Server  Elegant URL design  Custom Middleware  Authentication / Authorization  Internationalization support  Cache framework, with multiple cache mechanisms  Fast Development  Free, and Great Documentation 4
  • 5. Installation Prerequisites   Python PIP for installing Python packages (http://www.pip-installer.org/en/latest/installing.html)  pip install Django==1.5.4 o OR https://www.djangoproject.com/download/ - python setup.py install  pip install mysql-python o MySQL on windows https://pypi.python.org/pypi/MySQL-python/1.2.4  Add Python and Django to env path o PYTHONPATH D:Python27 o Path D:Python27; D:Python27Libsite-packages; D:Python27Libsite-packagesdjangobin;  Testing installation o shell> import django; django.VERSION; 5
  • 6. Django Architecture Models Views Templates Controller Describes your data Controls what users sees How user sees it URL dispatcher 6
  • 7. Project Directory Structure demosite/ ---------------------------------- Just a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like. manage.py ------------------------- A command-line utility that lets you interact with this Django project in various ways. Type python manage.py help. You should never have to edit this file. demosite/ ------------------------- Actual Python package for your project. Use this name to import anything inside it (e.g. import demosite.settings) __init__.py ----------------- A file required for Python to treat the demosite directory as a package. settings.py ----------------- Settings/configuration for this Django project urls.py ---------------------- Root URL config, the URLs for this Django project, provides mapping to views wsgi.py ---------------------- An entry-point for WSGI-compatible webservers to serve your project templates/ ----------------- HTML files , renders based on views. You can change to any dir, configurable in settings.py static/ ----------------------- CSS, JS, images.. etc, configurable in settings.py demoapp/ ----------------__init__.py -------urls.py -----------views.py ---------- Responsible for processing a user’s request and for returning the response models.py --------- A model is the single, definitive source of information about your data. Generally, each model maps to a single database table. admin.py ---------- It reads metadata in your model to provide a powerful and production-ready interface forms.py ----------- To create and manipulate form data 7
  • 8. Settings  Project settings.py overrides from <python>/Lib/site-packgaes/django/conf/global_settings.py  Set DJANGO_SETTINGS_MODULE for your Project, tells django which settings to be used. (demoproject.settings)  export/set DJANGO_SETTINGS_MODULE=demoproject.settings  For server mod_wsgi: os.environ['DJANGO_SETTINGS_MODULE'] = 'demoproject.settings'  DEBUG True or False  DATABASES ENGINE postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.. etc  ROOT_URLCONF  MEDIA_ROOT directory that will hold user-uploaded files  MEDIA_URL To serve media files  STATIC_ROOT To any server static files css, js.. and admin UI files (can add more dirs to STATICFILES_DIRS)  STATIC_URL To serve static files  TEMPLATE_DIRS Template directories Using settings in Python code from django.conf import settings if settings.DEBUG: # Do something 8
  • 9. Project / Site Creation  Creating new Project  django-admin.py startproject demoproject A project is a collection of applications  Creating new Application  python manage.py startapp demosite An application tries to provide a single, relatively self-contained set of related functions  Using the built-in web server  python manage.py runserver  python manage.py runserver 80 Runs by default at port 8000 It checks for any error and validate the models. Throws errors/warnings for any misconfigurations and invalid entries. 9
  • 10. URL Dispatcher / Patterns  Root URL should be configured in settings.py o  ROOT_URLCONF = 'app.urls' Syntax patterns(prefix, (regular expression, Python callback function [, optional dictionary [, optional name]]) ) Example: urlpatterns = patterns(' ', (r'^articles-year/$', 'mysite.news.views.articles_year'), ) Note: o No need to add a leading slash (/articles-year) o The 'r' in front of each regular expression string is optional but recommended. It tells Python that a string is "raw" -- that nothing in the string should be escaped. In python, the ‘’ backslash character in control chars must be escaped for regular expression use. Basically we have to add one more slash i.e t, b. To work around backslash plague, you can raw string, by prefixing the string with the letter r. 10
  • 11.  Can include other URLconf modules urlpatterns = patterns(' ', url(r'^support/', include('demoproject.support.urls')), )  Using Prefix urlpatterns = patterns(' ', (r'^articles/(d{4})/$', 'mysite.news.views.articles_year'), (r'^articles/(d{4})/(d{2})/$', 'mysite.news.views.articles_month'), ) Here mysite.news.views is common, so can be rewritten as follows urlpatterns = patterns('mysite.news.views', (r'^articles/(d{4})/$', 'articles_year'), (r'^articles/(d{4})/(d{2})/$', 'articles_month'), )  Passing extra arguments and Dictionary mapping patterns(' ', (r'^articles/(?P<year>d{4})/$', 'articles_year'), {'foo': 'bar'}), ) We can get the values in views.py as year='2005', foo='bar' 11
  • 12. Who uses Django?  BitBucket  DISQUS (serving 400 million people)  Pinterest  Instagram  dPaste  Mozilla (support.mozill, addons.mozilla)  NASA  PBS (Public Broadcasting Service)  The Washington Post, NY Times, LA Times, The Guardian  National Geographic, Discovery Channel 12