SlideShare a Scribd company logo
Django is a high-level Python Web framework that encourages rapid development
and clean, pragmatic design.

True! (it enocourages, doesn't force you)

      ●   Object Relational Mapper
      ●   Automatic Admin Interface
      ●   URL dispatcher
      ●   Template System
      ●   Cache System
      ●   Internationalisation
      ●   Forms, with data validatio0n
      ●   devlopment web server
Django - overview
 Typical scenario
                                               urls.py

                                                     views.py

         request                                                models.py
                                    request
                            web
client                     server
                                    response                                Database
          response
         (normally html)
models.py
●   This is the file that
    defines the objects /            Poll                         Choice
    ralationships. Django
    equivalent of SQL
    'create table'
    commands.
●   Can also define
    instance methods –         from django.db import models
    that act on one
                               class Poll(models.Model):
    instance of the object.        question = models.CharField(max_length=200)
                                   pub_date = models.DateTimeField('date published')

                               class Choice(models.Model):
                                   poll = models.ForeignKey(Poll)
                                   choice_text = models.CharField(max_length=200)
                                   votes = models.IntegerField(default=0)
models.py
                                                                  Poll                          Choice
from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)




[user]$ python manage.py synchdb



 BEGIN;
 CREATE TABLE "polls_poll" (
     "id" serial NOT NULL PRIMARY KEY,
     "question" varchar(200) NOT NULL,
     "pub_date" timestamp with time zone NOT NULL
 );
 CREATE TABLE "polls_choice" (
     "id" serial NOT NULL PRIMARY KEY,
     "poll_id" integer NOT NULL REFERENCES "polls_poll" ("id") DEFERRABLE INITIALLY DEFERRED,
     "choice_text" varchar(200) NOT NULL,
     "votes" integer NOT NULL
 );
 COMMIT;
Now we have an API to play with!

                                                                                        This saves writing a lot of
                                                                                        boiler plate SQL code, e.g.
  [user]$ python manage.py shell
                                                                                        INSERT's, UPDATE's, etc

>>> from polls.models import Poll, Choice   # Import the model classes we just wrote.

# No polls are in the system yet.
>>> Poll.objects.all()
[]

# Create a new Poll.
>>> from django.utils import timezone
>>> p = Poll(question="What's new?", pub_date=timezone.now())
>>> p.save()

# Now it has an ID. Note that this might say "1L" instead of "1", depending
# on which database you're using. That's no biggie; it just means your
# database backend prefers to return integers as Python long integer
# objects.
>>> p.id
1

# Access database columns via Python attributes.
>>> p.question
"What's new?"
>>> p.pub_date
datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=<UTC>)

# Change values by changing the attributes, then calling save().
>>> p.question = "What's up?"
>>> p.save()

# objects.all() displays all the polls in the database.
>>> Poll.objects.all()
[<Poll: Poll object>]
Admin Interface
“One of the most powerful parts of Django is the automatic admin interface. It reads metadata
in your model to provide a powerful and production-ready interface that content producers can
immediately use to start adding content to the site. In this document, we discuss how to
activate, use and customize Django’s admin interface.”


  ●   One of Django's best features.
  ●   Provides a web page to view / enter / update
      data, one per model (one per database table).
  ●   Automatically generated.
  ●   Lots of configuartion options.
  ●   Probably able to do a large chunk of what you
      want to do (for little effort)
admin.py
from polls.models import Poll, Choice
from django.contrib import admin

class ChoiceInline(admin.TabularInline):
    model = Choice
    extra = 3

class PollAdmin(admin.ModelAdmin):
    fieldsets = [
        (None, {'fields': ['question']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    list_display = ('question', 'pub_date', 'was_published_today')
    list_filter = ['pub_date']
    search_fields = ['question']
    date_hierarchy = 'pub_date'
    inlines = [ChoiceInline]

admin.site.register(Poll, PollAdmin)
Django - overview
                                               urls.py

                                                     views.py

         request                                                models.py
                                    request
                            web
client                     server
                                    response                                Database
          response
         (normally html)
urls.py
    ●   This is the file that tells django what code to execute based on the
        url
    ●   Uses regular expressions, and passes caturing groups to the view
from django.conf.urls.defaults import *
from django.conf.urls.static import static
from django.conf import settings
from sequencing import views
from django.contrib import admin
admin.autodiscover()


urlpatterns = patterns('',

        (r'^admin/doc/', include('django.contrib.admindocs.urls')),
        (r'admin/sequencing/lookup_values/(?P<table>S+)/(?P<field>S+)/' ,
                         'sequencing.views.lookup_values'), # goes to lookup_values
                                                            # in views.py, with paramters,
                                                            # self, table, and field

        (r'^admin/dates_view/' , DatesView.as_view()) , # example of a generic list view

)
views.py
●    So each view will take an HTTP request object, and return a HTTP
     response.
●    Usually you will return HTML, but other responses such as JSON or
     excel can be returned.


    A very basic example:

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the poll index.")
views.py and models.py together
from django.http import HttpResponse

from polls.models import Poll     # importing Poll object from models.py

def index(request):
    latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
    output = ', '.join([p.question for p in latest_poll_list])
    return HttpResponse(output)




  This line will generate SQL equivalent to:

  SELECT * FROM myapp__poll
  ORDER BY pub_date DESC
  LIMIT 5
Django - overview                              A view
                                                                           usually
                                                                           generates a
                                                                           queryset
                                              urls.py                      (from
                                                                           models), and
                                                    views.py               passes it to a
                                                                           template to
                                                                           generate a
         request                                               models.py   response
                                    request
                            web
client                     server

          response                                                          Database
         (normally html)
                                      response
views using a template
views.py
from django.http import HttpResponse
from django.template import Context, loader

from polls.models import Poll

def index(request):
    latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
    template = loader.get_template('polls/index.html')
    context = Context({
        'latest_poll_list': latest_poll_list,
    })
    return HttpResponse(template.render(context))




polls/index.html
{% if latest_poll_list %}
    <ul>
    {% for poll in latest_poll_list %}
        <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}
Good                                                   Bad
●   Very well doccumented                           ●   ORM is somehwat limted. For example
                                                        no contidtional agrregates.
●   Well known, lots of answers on Stack
    Overflow                                        ●   Template system is somewhat imited, no
                                                        logic in templates.
●   Lots of third party 'apps' (plugins), e.g
    Tastypie for REST, Django Debug                 ●   These things can be replaced, but then
    Toolbar, CMS pugins                                 you loose a lot of the magic that makes
                                                        it so great. For example replacing the
●   (Semi) automatically generated Admin                ORM with SQL alchemy is possible, but
    interface. May do most of what you need             then you loose the Admin interface.
    for very little effort (just configuration).
                                                    ●   Schema changes are a bit of a pain
                                                        (though to be fair, probably the same for
                                                        any DB application). South application
                                                        helps here.



                                                   so-so
                               ●   Deployment is not as easy as PHP,
                                   but there are a few options.
                               ●   Hosting not as ubiqutous. Though this
                                   is true for any Python framework

More Related Content

ODP
dJango
PDF
Building a Dynamic Website Using Django
PPTX
Django Framework Overview forNon-Python Developers
PDF
Introduction to django
PDF
Building Pluggable Web Applications using Django
PPTX
Django Architecture Introduction
PDF
Django Overview
dJango
Building a Dynamic Website Using Django
Django Framework Overview forNon-Python Developers
Introduction to django
Building Pluggable Web Applications using Django
Django Architecture Introduction
Django Overview

What's hot (20)

ODP
Django for Beginners
PDF
A Basic Django Introduction
PPTX
Tango with django
KEY
Jumpstart Django
PDF
Django a whirlwind tour
PDF
The Django Book - Chapter 5: Models
PPTX
The Django Web Application Framework 2
PDF
High Performance Django
PDF
Web develop in flask
KEY
Web application development with Django framework
KEY
PyCon US 2012 - State of WSGI 2
KEY
Making Django and NoSQL Play Nice
PDF
Python RESTful webservices with Python: Flask and Django solutions
PPTX
Flask – Python
KEY
LvivPy - Flask in details
PPTX
Django Girls Tutorial
PDF
Django Heresies
PPTX
An Overview of Models in Django
PDF
Django best practices for logging and signals
PDF
Introduction To Django (Strange Loop 2011)
Django for Beginners
A Basic Django Introduction
Tango with django
Jumpstart Django
Django a whirlwind tour
The Django Book - Chapter 5: Models
The Django Web Application Framework 2
High Performance Django
Web develop in flask
Web application development with Django framework
PyCon US 2012 - State of WSGI 2
Making Django and NoSQL Play Nice
Python RESTful webservices with Python: Flask and Django solutions
Flask – Python
LvivPy - Flask in details
Django Girls Tutorial
Django Heresies
An Overview of Models in Django
Django best practices for logging and signals
Introduction To Django (Strange Loop 2011)
Ad

Similar to Introduction to Django (20)

PDF
Introduction to Django
PDF
Introduction to Django
PDF
django_introduction20141030
PPTX
Web development with django - Basics Presentation
PDF
GDG Addis - An Introduction to Django and App Engine
KEY
Introduction Django
PDF
Python & Django TTT
PPTX
Hands on django part 1
PDF
CCCDjango2010.pdf
PDF
Django tutorial 2009
ODP
Django tech-talk
PDF
Django cheat sheet
PPTX
DJ-06-Views-Templates.pptx
ODP
Django Introduction Osscamp Delhi September 08 09 2007 Mir Nazim
PDF
Django workshop : let's make a blog
PPTX
PPTX
templates in Django material : Training available at Baabtra
PDF
Gae Meets Django
PPTX
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
KEY
Django class based views (Dutch Django meeting presentation)
Introduction to Django
Introduction to Django
django_introduction20141030
Web development with django - Basics Presentation
GDG Addis - An Introduction to Django and App Engine
Introduction Django
Python & Django TTT
Hands on django part 1
CCCDjango2010.pdf
Django tutorial 2009
Django tech-talk
Django cheat sheet
DJ-06-Views-Templates.pptx
Django Introduction Osscamp Delhi September 08 09 2007 Mir Nazim
Django workshop : let's make a blog
templates in Django material : Training available at Baabtra
Gae Meets Django
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
Django class based views (Dutch Django meeting presentation)
Ad

Recently uploaded (20)

PPTX
A Presentation on Artificial Intelligence
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Modernizing your data center with Dell and AMD
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Electronic commerce courselecture one. Pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
cuic standard and advanced reporting.pdf
A Presentation on Artificial Intelligence
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
NewMind AI Monthly Chronicles - July 2025
20250228 LYD VKU AI Blended-Learning.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Modernizing your data center with Dell and AMD
Network Security Unit 5.pdf for BCA BBA.
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Mobile App Security Testing_ A Comprehensive Guide.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Understanding_Digital_Forensics_Presentation.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Machine learning based COVID-19 study performance prediction
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Electronic commerce courselecture one. Pdf
MYSQL Presentation for SQL database connectivity
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Chapter 3 Spatial Domain Image Processing.pdf
cuic standard and advanced reporting.pdf

Introduction to Django

  • 1. Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. True! (it enocourages, doesn't force you) ● Object Relational Mapper ● Automatic Admin Interface ● URL dispatcher ● Template System ● Cache System ● Internationalisation ● Forms, with data validatio0n ● devlopment web server
  • 2. Django - overview Typical scenario urls.py views.py request models.py request web client server response Database response (normally html)
  • 3. models.py ● This is the file that defines the objects / Poll Choice ralationships. Django equivalent of SQL 'create table' commands. ● Can also define instance methods – from django.db import models that act on one class Poll(models.Model): instance of the object. question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') class Choice(models.Model): poll = models.ForeignKey(Poll) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0)
  • 4. models.py Poll Choice from django.db import models class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') class Choice(models.Model): poll = models.ForeignKey(Poll) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0) [user]$ python manage.py synchdb BEGIN; CREATE TABLE "polls_poll" ( "id" serial NOT NULL PRIMARY KEY, "question" varchar(200) NOT NULL, "pub_date" timestamp with time zone NOT NULL ); CREATE TABLE "polls_choice" ( "id" serial NOT NULL PRIMARY KEY, "poll_id" integer NOT NULL REFERENCES "polls_poll" ("id") DEFERRABLE INITIALLY DEFERRED, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL ); COMMIT;
  • 5. Now we have an API to play with! This saves writing a lot of boiler plate SQL code, e.g. [user]$ python manage.py shell INSERT's, UPDATE's, etc >>> from polls.models import Poll, Choice # Import the model classes we just wrote. # No polls are in the system yet. >>> Poll.objects.all() [] # Create a new Poll. >>> from django.utils import timezone >>> p = Poll(question="What's new?", pub_date=timezone.now()) >>> p.save() # Now it has an ID. Note that this might say "1L" instead of "1", depending # on which database you're using. That's no biggie; it just means your # database backend prefers to return integers as Python long integer # objects. >>> p.id 1 # Access database columns via Python attributes. >>> p.question "What's new?" >>> p.pub_date datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=<UTC>) # Change values by changing the attributes, then calling save(). >>> p.question = "What's up?" >>> p.save() # objects.all() displays all the polls in the database. >>> Poll.objects.all() [<Poll: Poll object>]
  • 6. Admin Interface “One of the most powerful parts of Django is the automatic admin interface. It reads metadata in your model to provide a powerful and production-ready interface that content producers can immediately use to start adding content to the site. In this document, we discuss how to activate, use and customize Django’s admin interface.” ● One of Django's best features. ● Provides a web page to view / enter / update data, one per model (one per database table). ● Automatically generated. ● Lots of configuartion options. ● Probably able to do a large chunk of what you want to do (for little effort)
  • 7. admin.py from polls.models import Poll, Choice from django.contrib import admin class ChoiceInline(admin.TabularInline): model = Choice extra = 3 class PollAdmin(admin.ModelAdmin): fieldsets = [ (None, {'fields': ['question']}), ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}), ] list_display = ('question', 'pub_date', 'was_published_today') list_filter = ['pub_date'] search_fields = ['question'] date_hierarchy = 'pub_date' inlines = [ChoiceInline] admin.site.register(Poll, PollAdmin)
  • 8. Django - overview urls.py views.py request models.py request web client server response Database response (normally html)
  • 9. urls.py ● This is the file that tells django what code to execute based on the url ● Uses regular expressions, and passes caturing groups to the view from django.conf.urls.defaults import * from django.conf.urls.static import static from django.conf import settings from sequencing import views from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', (r'^admin/doc/', include('django.contrib.admindocs.urls')), (r'admin/sequencing/lookup_values/(?P<table>S+)/(?P<field>S+)/' , 'sequencing.views.lookup_values'), # goes to lookup_values # in views.py, with paramters, # self, table, and field (r'^admin/dates_view/' , DatesView.as_view()) , # example of a generic list view )
  • 10. views.py ● So each view will take an HTTP request object, and return a HTTP response. ● Usually you will return HTML, but other responses such as JSON or excel can be returned. A very basic example: from django.http import HttpResponse def index(request): return HttpResponse("Hello, world. You're at the poll index.")
  • 11. views.py and models.py together from django.http import HttpResponse from polls.models import Poll # importing Poll object from models.py def index(request): latest_poll_list = Poll.objects.order_by('-pub_date')[:5] output = ', '.join([p.question for p in latest_poll_list]) return HttpResponse(output) This line will generate SQL equivalent to: SELECT * FROM myapp__poll ORDER BY pub_date DESC LIMIT 5
  • 12. Django - overview A view usually generates a queryset urls.py (from models), and views.py passes it to a template to generate a request models.py response request web client server response Database (normally html) response
  • 13. views using a template views.py from django.http import HttpResponse from django.template import Context, loader from polls.models import Poll def index(request): latest_poll_list = Poll.objects.order_by('-pub_date')[:5] template = loader.get_template('polls/index.html') context = Context({ 'latest_poll_list': latest_poll_list, }) return HttpResponse(template.render(context)) polls/index.html {% if latest_poll_list %} <ul> {% for poll in latest_poll_list %} <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li> {% endfor %} </ul> {% else %} <p>No polls are available.</p> {% endif %}
  • 14. Good Bad ● Very well doccumented ● ORM is somehwat limted. For example no contidtional agrregates. ● Well known, lots of answers on Stack Overflow ● Template system is somewhat imited, no logic in templates. ● Lots of third party 'apps' (plugins), e.g Tastypie for REST, Django Debug ● These things can be replaced, but then Toolbar, CMS pugins you loose a lot of the magic that makes it so great. For example replacing the ● (Semi) automatically generated Admin ORM with SQL alchemy is possible, but interface. May do most of what you need then you loose the Admin interface. for very little effort (just configuration). ● Schema changes are a bit of a pain (though to be fair, probably the same for any DB application). South application helps here. so-so ● Deployment is not as easy as PHP, but there are a few options. ● Hosting not as ubiqutous. Though this is true for any Python framework