SlideShare una empresa de Scribd logo
1 de 45
Descargar para leer sin conexión
Django on GAE course




                           Course summary




Version 0.1, 13-May-2010
Agenda
●   Python                           ●   Django
        –    Variables, Operators,           –    Mgmt commands
               Introspection                 –    Models, Admin
        –    Data structures &               –    URL's, Views,
              Control structures                   Templates
        –    List comprehension              –    Forms, Generic
        –    Functions, Classes                     Views, I18n
        –    Unit testing                    –    Unit testing
●   Google AppEngine                         –    Pluggable Apps
        –    Deploying Django projects using django-nonrel
        –    Limitations, Features, API's, BigTable datastore
Variables

●   No typing – data type inferred by assignment
       –   X=8
       –   Y = “abc”
●   Next assignment can be to a different data
    type
●   Under the hood, primitive variables are
    pointers to memory locations whose stored
    values don't change (just the pointer).
       –   Unlike Data Structures which point to memory
            locations that may change
Operators

●   Standard arithmetical, logical & bits operators
        –   12 + 7
        –   “s” * 8
        –   10 > 1
●   Objects of any class can support operators, by
    implementing internal methods, such as:
        –   __mul__, __add__, __div__, __pow__
        –   __gt__, __lt__, __eq__
        –   __xor__, __or__, __and__
Introspection

●   Everything in Python is an object
        –   Primitive values, methods, classes, modules
●   The type function returns the type of an object
●   The dir function returns the methods of an
    object
●   The hasattr method checks whether an object
    has some method
●   The __doc__ property returns the
    documentation of an object, e.g., a method
Data Structures

●   There are 4 main types:
       –   Lists: ordered collection of variables, of any
             type
               ●   l = [21, “ac”, 3.12, 21]
       –   Tuples: just like lists, but immutable
               ●   t = (21, “ac”, 3.12, 21)
       –   Dictionary (map): set of key-value pairs
               ●   d = {“x”: 12, “abc”: “de”, 12: “hh”}
       –   Set: unordered collection of unique variables
               ●   S = {21, “ac”, 3.12}
List Comprehension

●   Useful way to transform a collection into
    another
        –   e.g., suppose we want to multiply each
              element of a list by 2:
                ●   list2 = [x*2 for x in list1]
        –   We can also add condition filtering the
             elements, e.g., remove odd elements:
                ●   list2 = [x*2 for x in list1 if x mod 2 == 0]
        –   List comprehension can be nested too:
                ●   List3 = [x*2 for x in list2 for list2 in list1]
Unpacking

●   Unpacking means assigning the contents of a
    collection into stand-alone variables
       –   E.g., you can do:
                ●   x, y, z = [56, “bb”, 7]
●   There's also an operator for unpacking a
    sequence - * - e.g.:
       –   head, *tail = [87, 98, “kk”, 9.8]
                ●   head will contain 87, & tail: [98, “kk”, 9.8]
       –   first, *mid, last = “Charles Philip Arthur George
              Windsor”.split()
Control Structures

●   Standard idioms for controlling the flow of a
    program, such as condition, loop on list, loop
    until some condition &c.
●   The content of control structure blocks isn't
    surrounded by some sign (e.g., { }) but rather
    marked by being indented after the starting
    line, e.g.:
        –   If x > 0:
                print “Yay”
                y = 2/x
            else:
                print “Naaa”
Loops

●   Looping on lists is done like this:
        –   for el in li:
                 print el
        –   for i in range(10):
                  print i
●   You can also use unpacking:
        –     li = [(1, “w”), (2, “b”), (3, “f”)]
              for i, c in li:
                  print i, “=”, c
        –     for i, el in enumerate(list1):
                 print i, “ = “, el
Functions

●   Functions chunk complex logic into a named
    operation, with possible parameters:
       –   def add(x, y):
              print “add called with”, x, “, “, y
              return x + y
●   Parameters can have default values, & can be
    invoked by order or name:
       –   def move(direction, num_steps=1):
              ...

             move(direction=-30)
             move(180, 5)
Classes
String Formatting
Unit Testing
Reading from the Web
Files IO
Useful Libraries
Summary Examples in Shell
Management Commands

●   To start a project, run the startproject
    command:
        –   django-admin.py startproject myproj
●   Inside this folder, create a Pluggable App:
        –   python manage.py startapp myapp
●   To create the DB (configured it in settings.py):
        –   python manage.py syncdb
●   Now run the server:
        –   python manage.py runserver
Models

●   Models define the entities used in our
    application, & their business logic behavior
●   Models are entered in a file called models.py
●   class Book(models.Model):
       title = models.CharField(max_length=200)
       author = models.ForeignKey(Author)
       isbn = models.CharField(max_length=50)
Admin

●   Django arrives with an app for generating
    high-quality data entry GUI for your models
●   You need to add metadata for the admin app
    in a file called admin.py
       –   The file should contain a metadata class per
            any model entity you'll need to edit:
       –   class BookAdmin(admin.ModelAdmin):
               list_display = [“isbn”, “title”, “author”]
               search_fields = [“isbn”, “title”]
               list_filter = [“author”]
               ordering = [“author”, “title”]
URL's Configuration

●   Django encourages you to design meaningful
    URL's for your application pages & API
●   To do that, you need define the URL's patters
    in the file urls.py, & specify which controller
    handles each URL
Views

●   In Django speak, Views are actually the
    controllers responsible for handling requests
●   Views are defined in a file called views.py, &
    need to prepare all the data for the page that
    will be eventually returned
Templates

●   To actually render the result page, using the
    data prepared by the view, you need to write
    Templates
●   Templates are located in 1 or more folders,
    listed in the settings file
●   In order to maintain the DRY (Don't Repeat
    Yourself) principle, templates can inherit one
    another, in order for each one to add or
    override another, without duplicating anything
Forms

●   Django can generate data entry forms using
    simple metadata, that can possibly be inferred
    from the model definition of the entity being
    edited
●   Forms metadata are defined in a file called
    forms.py
Generic Views

●   Some views repeat themselves, so Django
    offers several basic views that can replace
    them, with parameters, such as:
        –   direct_to_template: view that just returns some
              template
        –   object_list: view that lists entities received by
             some query
        –   object_detail: view that presents the details of
             some entity
●   The parameters for a generic view are
    provided as a dictionary
I18n

●   Django makes it easy to offer your application
    in several languages. To do that:
        –   Wrap the strings you want to translate in your
             code with a special function (ugettext in
             code, of trans in templates)
        –   Run: python manage.py makemessages-l he
                ●   This will generate a file containing the strings
                     you need to translate
        –   Translate the .po file generated
        –   Run: python manage.py compile messages
        –   You can now switch language in the settings,
             or let the user switch language
Unit Testing

●   There are several ways to prepare unit tests
    for your Django code, in order to be able to
    monitor that nothing got broken following
    some code change.
●   A nice way is to invoke a view – without having
    s running server - & test its output or the
    model embedded inside it
        –   See the example test developed in the class
●   To run all tests defined in some app, run:
        –   python manage.py test myapp
Pluggable Apps
Serializers

●   Django comes with a mechanism to represent
    a collection of entities in a string, to be sent
    over the wire
        –   This is useful for API's, e.g., when another
             application wants to consume your data
●   The serializers mechanism supports few
    standard data protocols, such as XML & JSON
Reverse URL
Absolute URL
Settings
Serving Static Files
Dumping & Loading Data
Deploying using django-nonrel
Limitations
Features
API's
BigTable Datastore

Más contenido relacionado

La actualidad más candente

A Basic Django Introduction
A Basic Django IntroductionA Basic Django Introduction
A Basic Django Introduction
Ganga Ram
 

La actualidad más candente (20)

Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Django Documentation
Django DocumentationDjango Documentation
Django Documentation
 
Django Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python DevelopersDjango Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python Developers
 
Django Interview Questions and Answers
Django Interview Questions and AnswersDjango Interview Questions and Answers
Django Interview Questions and Answers
 
Behaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & DrupalBehaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & Drupal
 
EuroPython 2013 - Python3 TurboGears Training
EuroPython 2013 - Python3 TurboGears TrainingEuroPython 2013 - Python3 TurboGears Training
EuroPython 2013 - Python3 TurboGears Training
 
Drupal 8 - Corso frontend development
Drupal 8 - Corso frontend developmentDrupal 8 - Corso frontend development
Drupal 8 - Corso frontend development
 
Django - basics
Django - basicsDjango - basics
Django - basics
 
A Basic Django Introduction
A Basic Django IntroductionA Basic Django Introduction
A Basic Django Introduction
 
Slides
SlidesSlides
Slides
 
PyCon Korea 2019 REST API Document Generation
PyCon Korea 2019 REST API Document GenerationPyCon Korea 2019 REST API Document Generation
PyCon Korea 2019 REST API Document Generation
 
Virtual Bolt Workshop - March 16, 2020
Virtual Bolt Workshop - March 16, 2020Virtual Bolt Workshop - March 16, 2020
Virtual Bolt Workshop - March 16, 2020
 
RailsVsDjango
RailsVsDjangoRailsVsDjango
RailsVsDjango
 
Maven 3.0 at Øredev
Maven 3.0 at ØredevMaven 3.0 at Øredev
Maven 3.0 at Øredev
 
Virtual Bolt Workshop, 5 May 2020
Virtual Bolt Workshop, 5 May 2020Virtual Bolt Workshop, 5 May 2020
Virtual Bolt Workshop, 5 May 2020
 
Easily Manage Patching and Application Updates with Chocolatey + Puppet - Apr...
Easily Manage Patching and Application Updates with Chocolatey + Puppet - Apr...Easily Manage Patching and Application Updates with Chocolatey + Puppet - Apr...
Easily Manage Patching and Application Updates with Chocolatey + Puppet - Apr...
 
Quick flask an intro to flask
Quick flask   an intro to flaskQuick flask   an intro to flask
Quick flask an intro to flask
 
Acquia BLT for the Win, or How to speed up the project setup, development an...
Acquia BLT for the Win, or  How to speed up the project setup, development an...Acquia BLT for the Win, or  How to speed up the project setup, development an...
Acquia BLT for the Win, or How to speed up the project setup, development an...
 
Unit testing (eng)
Unit testing (eng)Unit testing (eng)
Unit testing (eng)
 
Ant User Guide
Ant User GuideAnt User Guide
Ant User Guide
 

Similar a Django course summary

Core Java Programming Language (JSE) : Chapter II - Object Oriented Programming.
Core Java Programming Language (JSE) : Chapter II - Object Oriented Programming.Core Java Programming Language (JSE) : Chapter II - Object Oriented Programming.
Core Java Programming Language (JSE) : Chapter II - Object Oriented Programming.
WebStackAcademy
 

Similar a Django course summary (20)

Python for web security - beginner
Python for web security - beginnerPython for web security - beginner
Python for web security - beginner
 
Django
DjangoDjango
Django
 
pythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptxpythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptx
 
(7) c sharp introduction_advanvced_features_part_ii
(7) c sharp introduction_advanvced_features_part_ii(7) c sharp introduction_advanvced_features_part_ii
(7) c sharp introduction_advanvced_features_part_ii
 
R programming for data science
R programming for data scienceR programming for data science
R programming for data science
 
Using Python Libraries.pdf
Using Python Libraries.pdfUsing Python Libraries.pdf
Using Python Libraries.pdf
 
Python training
Python trainingPython training
Python training
 
Drupal Coding Standards - do and don't
Drupal Coding Standards - do and don'tDrupal Coding Standards - do and don't
Drupal Coding Standards - do and don't
 
Clean code and refactoring
Clean code and refactoringClean code and refactoring
Clean code and refactoring
 
Control structures functions and modules in python programming
Control structures functions and modules in python programmingControl structures functions and modules in python programming
Control structures functions and modules in python programming
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
Turbogears2 tutorial to create mvc app
Turbogears2 tutorial to create mvc appTurbogears2 tutorial to create mvc app
Turbogears2 tutorial to create mvc app
 
R - the language
R - the languageR - the language
R - the language
 
Thinking in Components
Thinking in ComponentsThinking in Components
Thinking in Components
 
Core Java Programming Language (JSE) : Chapter II - Object Oriented Programming.
Core Java Programming Language (JSE) : Chapter II - Object Oriented Programming.Core Java Programming Language (JSE) : Chapter II - Object Oriented Programming.
Core Java Programming Language (JSE) : Chapter II - Object Oriented Programming.
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
 
pythonlibrariesandmodules-210530042906.docx
pythonlibrariesandmodules-210530042906.docxpythonlibrariesandmodules-210530042906.docx
pythonlibrariesandmodules-210530042906.docx
 
Python
PythonPython
Python
 
Interview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdfInterview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdf
 

Más de Udi Bauman (10)

13
1313
13
 
Intro to-django-for-media-companies
Intro to-django-for-media-companiesIntro to-django-for-media-companies
Intro to-django-for-media-companies
 
Django course final-project
Django course final-projectDjango course final-project
Django course final-project
 
Nonrelational Databases
Nonrelational DatabasesNonrelational Databases
Nonrelational Databases
 
Ship Early Ship Often With Django
Ship Early Ship Often With DjangoShip Early Ship Often With Django
Ship Early Ship Often With Django
 
Python Django Intro V0.1
Python Django Intro V0.1Python Django Intro V0.1
Python Django Intro V0.1
 
Large Scale Processing with Django
Large Scale Processing with DjangoLarge Scale Processing with Django
Large Scale Processing with Django
 
Django And Ajax
Django And AjaxDjango And Ajax
Django And Ajax
 
Udi Google Dev Day
Udi Google Dev DayUdi Google Dev Day
Udi Google Dev Day
 
Intro To Django
Intro To DjangoIntro To Django
Intro To Django
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

Django course summary

  • 1. Django on GAE course Course summary Version 0.1, 13-May-2010
  • 2. Agenda ● Python ● Django – Variables, Operators, – Mgmt commands Introspection – Models, Admin – Data structures & – URL's, Views, Control structures Templates – List comprehension – Forms, Generic – Functions, Classes Views, I18n – Unit testing – Unit testing ● Google AppEngine – Pluggable Apps – Deploying Django projects using django-nonrel – Limitations, Features, API's, BigTable datastore
  • 3.
  • 4. Variables ● No typing – data type inferred by assignment – X=8 – Y = “abc” ● Next assignment can be to a different data type ● Under the hood, primitive variables are pointers to memory locations whose stored values don't change (just the pointer). – Unlike Data Structures which point to memory locations that may change
  • 5. Operators ● Standard arithmetical, logical & bits operators – 12 + 7 – “s” * 8 – 10 > 1 ● Objects of any class can support operators, by implementing internal methods, such as: – __mul__, __add__, __div__, __pow__ – __gt__, __lt__, __eq__ – __xor__, __or__, __and__
  • 6. Introspection ● Everything in Python is an object – Primitive values, methods, classes, modules ● The type function returns the type of an object ● The dir function returns the methods of an object ● The hasattr method checks whether an object has some method ● The __doc__ property returns the documentation of an object, e.g., a method
  • 7. Data Structures ● There are 4 main types: – Lists: ordered collection of variables, of any type ● l = [21, “ac”, 3.12, 21] – Tuples: just like lists, but immutable ● t = (21, “ac”, 3.12, 21) – Dictionary (map): set of key-value pairs ● d = {“x”: 12, “abc”: “de”, 12: “hh”} – Set: unordered collection of unique variables ● S = {21, “ac”, 3.12}
  • 8. List Comprehension ● Useful way to transform a collection into another – e.g., suppose we want to multiply each element of a list by 2: ● list2 = [x*2 for x in list1] – We can also add condition filtering the elements, e.g., remove odd elements: ● list2 = [x*2 for x in list1 if x mod 2 == 0] – List comprehension can be nested too: ● List3 = [x*2 for x in list2 for list2 in list1]
  • 9. Unpacking ● Unpacking means assigning the contents of a collection into stand-alone variables – E.g., you can do: ● x, y, z = [56, “bb”, 7] ● There's also an operator for unpacking a sequence - * - e.g.: – head, *tail = [87, 98, “kk”, 9.8] ● head will contain 87, & tail: [98, “kk”, 9.8] – first, *mid, last = “Charles Philip Arthur George Windsor”.split()
  • 10. Control Structures ● Standard idioms for controlling the flow of a program, such as condition, loop on list, loop until some condition &c. ● The content of control structure blocks isn't surrounded by some sign (e.g., { }) but rather marked by being indented after the starting line, e.g.: – If x > 0: print “Yay” y = 2/x else: print “Naaa”
  • 11. Loops ● Looping on lists is done like this: – for el in li: print el – for i in range(10): print i ● You can also use unpacking: – li = [(1, “w”), (2, “b”), (3, “f”)] for i, c in li: print i, “=”, c – for i, el in enumerate(list1): print i, “ = “, el
  • 12. Functions ● Functions chunk complex logic into a named operation, with possible parameters: – def add(x, y): print “add called with”, x, “, “, y return x + y ● Parameters can have default values, & can be invoked by order or name: – def move(direction, num_steps=1): ... move(direction=-30) move(180, 5)
  • 20.
  • 21.
  • 22.
  • 23. Management Commands ● To start a project, run the startproject command: – django-admin.py startproject myproj ● Inside this folder, create a Pluggable App: – python manage.py startapp myapp ● To create the DB (configured it in settings.py): – python manage.py syncdb ● Now run the server: – python manage.py runserver
  • 24. Models ● Models define the entities used in our application, & their business logic behavior ● Models are entered in a file called models.py ● class Book(models.Model): title = models.CharField(max_length=200) author = models.ForeignKey(Author) isbn = models.CharField(max_length=50)
  • 25. Admin ● Django arrives with an app for generating high-quality data entry GUI for your models ● You need to add metadata for the admin app in a file called admin.py – The file should contain a metadata class per any model entity you'll need to edit: – class BookAdmin(admin.ModelAdmin): list_display = [“isbn”, “title”, “author”] search_fields = [“isbn”, “title”] list_filter = [“author”] ordering = [“author”, “title”]
  • 26. URL's Configuration ● Django encourages you to design meaningful URL's for your application pages & API ● To do that, you need define the URL's patters in the file urls.py, & specify which controller handles each URL
  • 27. Views ● In Django speak, Views are actually the controllers responsible for handling requests ● Views are defined in a file called views.py, & need to prepare all the data for the page that will be eventually returned
  • 28. Templates ● To actually render the result page, using the data prepared by the view, you need to write Templates ● Templates are located in 1 or more folders, listed in the settings file ● In order to maintain the DRY (Don't Repeat Yourself) principle, templates can inherit one another, in order for each one to add or override another, without duplicating anything
  • 29. Forms ● Django can generate data entry forms using simple metadata, that can possibly be inferred from the model definition of the entity being edited ● Forms metadata are defined in a file called forms.py
  • 30. Generic Views ● Some views repeat themselves, so Django offers several basic views that can replace them, with parameters, such as: – direct_to_template: view that just returns some template – object_list: view that lists entities received by some query – object_detail: view that presents the details of some entity ● The parameters for a generic view are provided as a dictionary
  • 31. I18n ● Django makes it easy to offer your application in several languages. To do that: – Wrap the strings you want to translate in your code with a special function (ugettext in code, of trans in templates) – Run: python manage.py makemessages-l he ● This will generate a file containing the strings you need to translate – Translate the .po file generated – Run: python manage.py compile messages – You can now switch language in the settings, or let the user switch language
  • 32. Unit Testing ● There are several ways to prepare unit tests for your Django code, in order to be able to monitor that nothing got broken following some code change. ● A nice way is to invoke a view – without having s running server - & test its output or the model embedded inside it – See the example test developed in the class ● To run all tests defined in some app, run: – python manage.py test myapp
  • 34. Serializers ● Django comes with a mechanism to represent a collection of entities in a string, to be sent over the wire – This is useful for API's, e.g., when another application wants to consume your data ● The serializers mechanism supports few standard data protocols, such as XML & JSON
  • 40.
  • 44. API's