SlideShare una empresa de Scribd logo
1 de 26
Descargar para leer sin conexión
What is Django?
        Features
     References




Introducing Django

      Horst Gutmann


   September 30, 2007




                        1 / 34
What is Django?
                                Features
                             References


Disclaimer




   I’m not of the developers of Django but merely a user who likes
   what he’s seen. My experience is limited to small sites that can be
   comfortably run on a shared host such as Dreamhost.
   And I want to apologize for the noise my laptop is probably going
   to make during the presentation ;-)




                                                                         2 / 34
What is Django?
                                                   General
                                        Features
                                                   What do you need?
                                     References


What is Django?

                  Django is a high-level Python Web framework that
              encourages rapid development and clean, pragmatic
              design.
   [1]
              Model-View-Controller for the Web
              Written in Python
              Explicit instead of implicit
              Developed for the Lawrence Journal-World1 newspaper in
              Kansas


         1
             http://www2.ljworld.com/
                                                                       3 / 34
What is Django?
                                    General
                         Features
                                    What do you need?
                      References


What is an MVC web framework?




  Abstraction




                                                        5 / 34
What is Django?
                                        General
                             Features
                                        What do you need?
                          References


Abstract HTTP




     Do we really want to manually parse HTTP requests?
     Frameworks should automate things like header creation
     Allow easy cookie handling




                                                              6 / 34
What is Django?
                                               General
                                    Features
                                               What do you need?
                                 References


Abstract database calls



   Page . o b j e c t s . g e t ( i d =10)
   instead of
   SELECT ∗ FROM page WHERE i d =10;
   Not to mention keeping it backend independent and mapping the
   resultset to objects in the used programming language.




                                                                   8 / 34
What is Django?
                                                  General
                                       Features
                                                  What do you need?
                                    References


Dispatch URLs



        URLs or URL patterns should be mapped to functions or
        objects
        ... that then create the output.

   u r l p a t t e r n s += p a t t e r n s ( ’ ’ ,
           u r l ( ’ ˆ$ ’ , ’ v i e w s . i n d e x ’ ) ,
   )




                                                                      10 / 34
What is Django?
                                          General
                               Features
                                          What do you need?
                            References


MVC or MVT?


  Django uses a little bit different naming for the
  Model-View-Controller pattern.
       Models abstract the used data by defining classes for them
              and storing them in relational tables.
        Views take the job of the controllers in MVC and basically
              define, what the user gets to see. Functions, not
              classes here.
    Templates define how the users see the view




                                                                     11 / 34
What is Django?
                                           General
                                Features
                                           What do you need?
                             References


Projects and Applications



        Project A project is for example your whole website. Here
                you store your central configuration and general
                templates and images (just as an example).
   Applications are where you store the actual functionality. For
                example a weblog would be an application. An
                account system would be an application ...
   Applications are a simple way to share common functionality
   between various projects.




                                                                    12 / 34
What is Django?
                                              General
                                   Features
                                              What do you need?
                                References


Easy start




          Python2 (ideally 2.5 since sqlite is bundled with it)
          a text editor
   This is all you need to start developing with Django since Django
   comes with its own lightweight webserver to make it very easy for
   everyone to start playing around with it.




     2
         http://www.python.org
                                                                       13 / 34
What is Django?
                                      General
                           Features
                                      What do you need?
                        References


Yet flexible




                                              SQLite
      FastCGI
                                              MySQL
      mod python
                                              PostgreSQL
      mod wsgi
                                              Oracle in preparation




                                                                      14 / 34
What is Django?    Contributed apps
                               Features   The Template Language
                            References    Simple Form Processing


Main features (for me)



      A good collection of contributed applications
          Administration interface
          Authentication system
          Comments system
          ...
      Template language focused on inheritance
      Simple form processing
      No magic




                                                                   15 / 34
What is Django?    Contributed apps
                            Features   The Template Language
                         References    Simple Form Processing


Administration interface I




                                                                16 / 34
What is Django?    Contributed apps
                               Features   The Template Language
                            References    Simple Form Processing


Administration interface II




       Available in django.contrib.admin
       Configured through model definitions
       Configurable using templates (possible per model),
       JavaScripts and CSS
       Currently a rewrite using newforms is in the pipe.




                                                                   17 / 34
What is Django?    Contributed apps
                                   Features   The Template Language
                                References    Simple Form Processing



Authentication system3




          django.contrib.auth
          Just hook it in, and you get a login screen
          ... and usergroups
          ... and custom permissions




     3
         http://www.djangoproject.com/documentation/authentication/
                                                                       18 / 34
What is Django?    Contributed apps
                                   Features   The Template Language
                                References    Simple Form Processing



django-registration5



          By James Bennett4
          Offers all you really need:
              Simple registration form
              E-mail activation
          Works with django.contrib.auth




     4
         http://www.b-list.org/about/
     5
         http://code.google.com/p/django-registration/
                                                                       19 / 34
What is Django?    Contributed apps
                                 Features   The Template Language
                              References    Simple Form Processing


The Template Language



      Very restrictive
           ... to keep application logic within the View and the Model
           Don’t let templates change the state of your site!
           If you want to have complex code in your templates, you have
           to define your own template tags in Python.
      Inheritance between templates
      You have to pass variables explicitly to the template from the
      view




                                                                          20 / 34
What is Django?    Contributed apps
                                 Features   The Template Language
                              References    Simple Form Processing


Template Basics

     Variables Rendered with
              {{ variable_name }}



        Tags allow you to do more sophisticated stuff while
             keeping the actual Python code out of the template:
              {% template_tag %}



              Even conditional blocks are realized as tags.
       Filters allow you to manipulate the output of a variable:
              {{ variable|filter }}



              E.g.: Escaping strings, converting from Markdown to
              HTML, ...

                                                                     22 / 34
What is Django?    Contributed apps
                                Features   The Template Language
                             References    Simple Form Processing



Template Inheritance6
  The core of inheritance are blocks the templates.
  base.html                            index.html
  <html>                                    {% extends ’base.html’ %}
    <body>                                  {% block title %}
      <h1>                                      Index page
        {% block title %}                   {% endblock %}
        {% endblock %}                      {% block content %}
      </h1>                                     ...
      <div id=quot;contentquot;>                    {% endblock %}
        {% block content %}
        {% endblock %}
      </div>
    </body>
  </html>
     6
       http://www.djangoproject.com/documentation/templates/
   #template-inheritance
                                                                        24 / 34
What is Django?    Contributed apps
                                  Features   The Template Language
                               References    Simple Form Processing



newforms7


         ”newforms” because there was something before that ... let’s
         ignore the only form processing for now ;-)
         Easy way to define forms
         ... to render them
         ... and to validate their data
         Easily combinable with models (as you will see in the
         demoapp)




    7
        http://www.djangoproject.com/documentation/newforms/
                                                                        25 / 34
What is Django?    Contributed apps
                                     Features   The Template Language
                                  References    Simple Form Processing


Form definition



   from d j a n g o import newforms a s f o r m s
   c l a s s CommentForm ( m o d e l s . Form ) :
           a u t h o r = form . C h a r F i e l d ( ’ Your name ’ )
           e m a i l = form . E m a i l F i e l d ( ’ Your e−m a i l )
           body = form . C h a r F i e l d ( ’ Comment ’ ,
                   w i d g e t=f o r m s . T e x t a r e a ( ) )




                                                                         27 / 34
What is Django?          Contributed apps
                                                 Features         The Template Language
                                              References          Simple Form Processing


Form output




   {{ form . a s p }}
  Results in:
  <p> a b e l f o r=” i d a u t h o r ”>Your name :</ l a b e l>
     <l
      <i n p u t i d=” i d a u t h o r ” t y p e=” t e x t ” name=” a u t h o r ” m a x l e n g t h=” 100 ” /></p>
  <p> a b e l f o r=” i d e m a i l ”>Your e−m a i l :</ l a b e l>
     <l
      <i n p u t i d=” i d e m a i l ” t y p e=” t e x t ” name=” e m a i l ” m a x l e n g t h=” 75 ” />  </p>
  <p> a b e l f o r=” i d b o d y ”>Comment :</ l a b e l>
     <l
      <t e x t a r e a i d=” i d b o d y ” rows=” 10 ” c o l s=” 40 ” name=” body ”> t e x t a r e a>
                                                                                               </             </p>




                                                                                                                     29 / 34
What is Django?    Contributed apps
                                      Features   The Template Language
                                   References    Simple Form Processing


Form data validation


   Everything is done in Python, so it’s easily re-usable.
   c l a s s CommentForm ( f o r m s . Form ) :

     # ...

     def c l e a n e m a i l ( s e l f ) :
       e m a i l = s e l f . c l e a n e d d a t a [ ” e m a i l ” ] . s p l i t ( ”@” ) [ 1 ]
       i f e m a i l i n [ ” h o t m a i l . com” , ” yahoo . com” ] :
          r a i s e V a l i d a t i o n E r r o r , ” Get a GMail a c c o u n t ! ”




                                                                                                 31 / 34
What is Django?
                         Features
                      References




“Django project site.” [Online]. Available:
http://www.djangoproject.com/




                                              32 / 34
What is Django?
                                Features
                             References


Thank you




  for your attention.
  Any last question?




                                           33 / 34
What is Django?
                               Features
                            References


Want to get some action?




   If you liked what you saw, we could meet somewhere and build a
   little website together :-)




                                                                    34 / 34

Más contenido relacionado

Similar a Introducing Django

Django Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python DevelopersDjango Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python DevelopersRosario Renga
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to DjangoAhmed Salama
 
* DJANGO - The Python Framework - Low Kian Seong, Developer
    * DJANGO - The Python Framework - Low Kian Seong, Developer    * DJANGO - The Python Framework - Low Kian Seong, Developer
* DJANGO - The Python Framework - Low Kian Seong, DeveloperLinuxmalaysia Malaysia
 
Why Django is The Go-To Framework For Python.pdf
Why Django is The Go-To Framework For Python.pdfWhy Django is The Go-To Framework For Python.pdf
Why Django is The Go-To Framework For Python.pdfMindfire LLC
 
Django interview Questions| Edureka
Django interview  Questions| EdurekaDjango interview  Questions| Edureka
Django interview Questions| EdurekaEdureka!
 
Company Visitor Management System Report.docx
Company Visitor Management System Report.docxCompany Visitor Management System Report.docx
Company Visitor Management System Report.docxfantabulous2024
 
Concepts and applications of Django.pptx
Concepts and applications of Django.pptxConcepts and applications of Django.pptx
Concepts and applications of Django.pptxsushmitjivtode4
 
Django Workflow and Architecture
Django Workflow and ArchitectureDjango Workflow and Architecture
Django Workflow and ArchitectureAndolasoft Inc
 
Introduction to django framework
Introduction to django frameworkIntroduction to django framework
Introduction to django frameworkKnoldus Inc.
 
Web Development in Django
Web Development in DjangoWeb Development in Django
Web Development in DjangoLakshman Prasad
 
Django Tutorial_ Let’s take a deep dive into Django’s web framework.pdf
Django Tutorial_ Let’s take a deep dive into Django’s web framework.pdfDjango Tutorial_ Let’s take a deep dive into Django’s web framework.pdf
Django Tutorial_ Let’s take a deep dive into Django’s web framework.pdfSudhanshiBakre1
 

Similar a Introducing Django (20)

Django Introdcution
Django IntrodcutionDjango Introdcution
Django Introdcution
 
Django
Django Django
Django
 
Django Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python DevelopersDjango Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python Developers
 
Django
DjangoDjango
Django
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
* DJANGO - The Python Framework - Low Kian Seong, Developer
    * DJANGO - The Python Framework - Low Kian Seong, Developer    * DJANGO - The Python Framework - Low Kian Seong, Developer
* DJANGO - The Python Framework - Low Kian Seong, Developer
 
Why Django is The Go-To Framework For Python.pdf
Why Django is The Go-To Framework For Python.pdfWhy Django is The Go-To Framework For Python.pdf
Why Django is The Go-To Framework For Python.pdf
 
Django
DjangoDjango
Django
 
Django interview Questions| Edureka
Django interview  Questions| EdurekaDjango interview  Questions| Edureka
Django interview Questions| Edureka
 
Django by rj
Django by rjDjango by rj
Django by rj
 
Company Visitor Management System Report.docx
Company Visitor Management System Report.docxCompany Visitor Management System Report.docx
Company Visitor Management System Report.docx
 
Django PPT.pptx
Django PPT.pptxDjango PPT.pptx
Django PPT.pptx
 
Concepts and applications of Django.pptx
Concepts and applications of Django.pptxConcepts and applications of Django.pptx
Concepts and applications of Django.pptx
 
Django Workflow and Architecture
Django Workflow and ArchitectureDjango Workflow and Architecture
Django Workflow and Architecture
 
Introduction to django framework
Introduction to django frameworkIntroduction to django framework
Introduction to django framework
 
Django
DjangoDjango
Django
 
Web Development in Django
Web Development in DjangoWeb Development in Django
Web Development in Django
 
Basic Python Django
Basic Python DjangoBasic Python Django
Basic Python Django
 
Dojango
DojangoDojango
Dojango
 
Django Tutorial_ Let’s take a deep dive into Django’s web framework.pdf
Django Tutorial_ Let’s take a deep dive into Django’s web framework.pdfDjango Tutorial_ Let’s take a deep dive into Django’s web framework.pdf
Django Tutorial_ Let’s take a deep dive into Django’s web framework.pdf
 

Último

Cannabis Legalization World Map: 2024 Updated
Cannabis Legalization World Map: 2024 UpdatedCannabis Legalization World Map: 2024 Updated
Cannabis Legalization World Map: 2024 UpdatedCannaBusinessPlans
 
Falcon Invoice Discounting: Aviate Your Cash Flow Challenges
Falcon Invoice Discounting: Aviate Your Cash Flow ChallengesFalcon Invoice Discounting: Aviate Your Cash Flow Challenges
Falcon Invoice Discounting: Aviate Your Cash Flow Challengeshemanthkumar470700
 
Over the Top (OTT) Market Size & Growth Outlook 2024-2030
Over the Top (OTT) Market Size & Growth Outlook 2024-2030Over the Top (OTT) Market Size & Growth Outlook 2024-2030
Over the Top (OTT) Market Size & Growth Outlook 2024-2030tarushabhavsar
 
Pre Engineered Building Manufacturers Hyderabad.pptx
Pre Engineered  Building Manufacturers Hyderabad.pptxPre Engineered  Building Manufacturers Hyderabad.pptx
Pre Engineered Building Manufacturers Hyderabad.pptxRoofing Contractor
 
Falcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon investment
 
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdfDr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdfAdmir Softic
 
Buy Verified TransferWise Accounts From Seosmmearth
Buy Verified TransferWise Accounts From SeosmmearthBuy Verified TransferWise Accounts From Seosmmearth
Buy Verified TransferWise Accounts From SeosmmearthBuy Verified Binance Account
 
Putting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptxPutting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptxCynthia Clay
 
Lucknow Housewife Escorts by Sexy Bhabhi Service 8250092165
Lucknow Housewife Escorts  by Sexy Bhabhi Service 8250092165Lucknow Housewife Escorts  by Sexy Bhabhi Service 8250092165
Lucknow Housewife Escorts by Sexy Bhabhi Service 8250092165meghakumariji156
 
Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024Marel
 
Structuring and Writing DRL Mckinsey (1).pdf
Structuring and Writing DRL Mckinsey (1).pdfStructuring and Writing DRL Mckinsey (1).pdf
Structuring and Writing DRL Mckinsey (1).pdflaloo_007
 
Power point presentation on enterprise performance management
Power point presentation on enterprise performance managementPower point presentation on enterprise performance management
Power point presentation on enterprise performance managementVaishnaviGunji
 
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...Falcon Invoice Discounting
 
PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPanhandleOilandGas
 
Cracking the 'Career Pathing' Slideshare
Cracking the 'Career Pathing' SlideshareCracking the 'Career Pathing' Slideshare
Cracking the 'Career Pathing' SlideshareWorkforce Group
 
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai KuwaitThe Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwaitdaisycvs
 
TVB_The Vietnam Believer Newsletter_May 6th, 2024_ENVol. 006.pdf
TVB_The Vietnam Believer Newsletter_May 6th, 2024_ENVol. 006.pdfTVB_The Vietnam Believer Newsletter_May 6th, 2024_ENVol. 006.pdf
TVB_The Vietnam Believer Newsletter_May 6th, 2024_ENVol. 006.pdfbelieveminhh
 
Falcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to ProsperityFalcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to Prosperityhemanthkumar470700
 

Último (20)

Cannabis Legalization World Map: 2024 Updated
Cannabis Legalization World Map: 2024 UpdatedCannabis Legalization World Map: 2024 Updated
Cannabis Legalization World Map: 2024 Updated
 
Falcon Invoice Discounting: Aviate Your Cash Flow Challenges
Falcon Invoice Discounting: Aviate Your Cash Flow ChallengesFalcon Invoice Discounting: Aviate Your Cash Flow Challenges
Falcon Invoice Discounting: Aviate Your Cash Flow Challenges
 
Over the Top (OTT) Market Size & Growth Outlook 2024-2030
Over the Top (OTT) Market Size & Growth Outlook 2024-2030Over the Top (OTT) Market Size & Growth Outlook 2024-2030
Over the Top (OTT) Market Size & Growth Outlook 2024-2030
 
Pre Engineered Building Manufacturers Hyderabad.pptx
Pre Engineered  Building Manufacturers Hyderabad.pptxPre Engineered  Building Manufacturers Hyderabad.pptx
Pre Engineered Building Manufacturers Hyderabad.pptx
 
!~+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUD...
!~+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUD...!~+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUD...
!~+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUD...
 
Falcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business Growth
 
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdfDr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
 
Buy Verified TransferWise Accounts From Seosmmearth
Buy Verified TransferWise Accounts From SeosmmearthBuy Verified TransferWise Accounts From Seosmmearth
Buy Verified TransferWise Accounts From Seosmmearth
 
Putting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptxPutting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptx
 
Lucknow Housewife Escorts by Sexy Bhabhi Service 8250092165
Lucknow Housewife Escorts  by Sexy Bhabhi Service 8250092165Lucknow Housewife Escorts  by Sexy Bhabhi Service 8250092165
Lucknow Housewife Escorts by Sexy Bhabhi Service 8250092165
 
Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024
 
Structuring and Writing DRL Mckinsey (1).pdf
Structuring and Writing DRL Mckinsey (1).pdfStructuring and Writing DRL Mckinsey (1).pdf
Structuring and Writing DRL Mckinsey (1).pdf
 
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabiunwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
 
Power point presentation on enterprise performance management
Power point presentation on enterprise performance managementPower point presentation on enterprise performance management
Power point presentation on enterprise performance management
 
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
 
PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation Final
 
Cracking the 'Career Pathing' Slideshare
Cracking the 'Career Pathing' SlideshareCracking the 'Career Pathing' Slideshare
Cracking the 'Career Pathing' Slideshare
 
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai KuwaitThe Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
 
TVB_The Vietnam Believer Newsletter_May 6th, 2024_ENVol. 006.pdf
TVB_The Vietnam Believer Newsletter_May 6th, 2024_ENVol. 006.pdfTVB_The Vietnam Believer Newsletter_May 6th, 2024_ENVol. 006.pdf
TVB_The Vietnam Believer Newsletter_May 6th, 2024_ENVol. 006.pdf
 
Falcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to ProsperityFalcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to Prosperity
 

Introducing Django

  • 1. What is Django? Features References Introducing Django Horst Gutmann September 30, 2007 1 / 34
  • 2. What is Django? Features References Disclaimer I’m not of the developers of Django but merely a user who likes what he’s seen. My experience is limited to small sites that can be comfortably run on a shared host such as Dreamhost. And I want to apologize for the noise my laptop is probably going to make during the presentation ;-) 2 / 34
  • 3. What is Django? General Features What do you need? References What is Django? Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. [1] Model-View-Controller for the Web Written in Python Explicit instead of implicit Developed for the Lawrence Journal-World1 newspaper in Kansas 1 http://www2.ljworld.com/ 3 / 34
  • 4. What is Django? General Features What do you need? References What is an MVC web framework? Abstraction 5 / 34
  • 5. What is Django? General Features What do you need? References Abstract HTTP Do we really want to manually parse HTTP requests? Frameworks should automate things like header creation Allow easy cookie handling 6 / 34
  • 6. What is Django? General Features What do you need? References Abstract database calls Page . o b j e c t s . g e t ( i d =10) instead of SELECT ∗ FROM page WHERE i d =10; Not to mention keeping it backend independent and mapping the resultset to objects in the used programming language. 8 / 34
  • 7. What is Django? General Features What do you need? References Dispatch URLs URLs or URL patterns should be mapped to functions or objects ... that then create the output. u r l p a t t e r n s += p a t t e r n s ( ’ ’ , u r l ( ’ ˆ$ ’ , ’ v i e w s . i n d e x ’ ) , ) 10 / 34
  • 8. What is Django? General Features What do you need? References MVC or MVT? Django uses a little bit different naming for the Model-View-Controller pattern. Models abstract the used data by defining classes for them and storing them in relational tables. Views take the job of the controllers in MVC and basically define, what the user gets to see. Functions, not classes here. Templates define how the users see the view 11 / 34
  • 9. What is Django? General Features What do you need? References Projects and Applications Project A project is for example your whole website. Here you store your central configuration and general templates and images (just as an example). Applications are where you store the actual functionality. For example a weblog would be an application. An account system would be an application ... Applications are a simple way to share common functionality between various projects. 12 / 34
  • 10. What is Django? General Features What do you need? References Easy start Python2 (ideally 2.5 since sqlite is bundled with it) a text editor This is all you need to start developing with Django since Django comes with its own lightweight webserver to make it very easy for everyone to start playing around with it. 2 http://www.python.org 13 / 34
  • 11. What is Django? General Features What do you need? References Yet flexible SQLite FastCGI MySQL mod python PostgreSQL mod wsgi Oracle in preparation 14 / 34
  • 12. What is Django? Contributed apps Features The Template Language References Simple Form Processing Main features (for me) A good collection of contributed applications Administration interface Authentication system Comments system ... Template language focused on inheritance Simple form processing No magic 15 / 34
  • 13. What is Django? Contributed apps Features The Template Language References Simple Form Processing Administration interface I 16 / 34
  • 14. What is Django? Contributed apps Features The Template Language References Simple Form Processing Administration interface II Available in django.contrib.admin Configured through model definitions Configurable using templates (possible per model), JavaScripts and CSS Currently a rewrite using newforms is in the pipe. 17 / 34
  • 15. What is Django? Contributed apps Features The Template Language References Simple Form Processing Authentication system3 django.contrib.auth Just hook it in, and you get a login screen ... and usergroups ... and custom permissions 3 http://www.djangoproject.com/documentation/authentication/ 18 / 34
  • 16. What is Django? Contributed apps Features The Template Language References Simple Form Processing django-registration5 By James Bennett4 Offers all you really need: Simple registration form E-mail activation Works with django.contrib.auth 4 http://www.b-list.org/about/ 5 http://code.google.com/p/django-registration/ 19 / 34
  • 17. What is Django? Contributed apps Features The Template Language References Simple Form Processing The Template Language Very restrictive ... to keep application logic within the View and the Model Don’t let templates change the state of your site! If you want to have complex code in your templates, you have to define your own template tags in Python. Inheritance between templates You have to pass variables explicitly to the template from the view 20 / 34
  • 18. What is Django? Contributed apps Features The Template Language References Simple Form Processing Template Basics Variables Rendered with {{ variable_name }} Tags allow you to do more sophisticated stuff while keeping the actual Python code out of the template: {% template_tag %} Even conditional blocks are realized as tags. Filters allow you to manipulate the output of a variable: {{ variable|filter }} E.g.: Escaping strings, converting from Markdown to HTML, ... 22 / 34
  • 19. What is Django? Contributed apps Features The Template Language References Simple Form Processing Template Inheritance6 The core of inheritance are blocks the templates. base.html index.html <html> {% extends ’base.html’ %} <body> {% block title %} <h1> Index page {% block title %} {% endblock %} {% endblock %} {% block content %} </h1> ... <div id=quot;contentquot;> {% endblock %} {% block content %} {% endblock %} </div> </body> </html> 6 http://www.djangoproject.com/documentation/templates/ #template-inheritance 24 / 34
  • 20. What is Django? Contributed apps Features The Template Language References Simple Form Processing newforms7 ”newforms” because there was something before that ... let’s ignore the only form processing for now ;-) Easy way to define forms ... to render them ... and to validate their data Easily combinable with models (as you will see in the demoapp) 7 http://www.djangoproject.com/documentation/newforms/ 25 / 34
  • 21. What is Django? Contributed apps Features The Template Language References Simple Form Processing Form definition from d j a n g o import newforms a s f o r m s c l a s s CommentForm ( m o d e l s . Form ) : a u t h o r = form . C h a r F i e l d ( ’ Your name ’ ) e m a i l = form . E m a i l F i e l d ( ’ Your e−m a i l ) body = form . C h a r F i e l d ( ’ Comment ’ , w i d g e t=f o r m s . T e x t a r e a ( ) ) 27 / 34
  • 22. What is Django? Contributed apps Features The Template Language References Simple Form Processing Form output {{ form . a s p }} Results in: <p> a b e l f o r=” i d a u t h o r ”>Your name :</ l a b e l> <l <i n p u t i d=” i d a u t h o r ” t y p e=” t e x t ” name=” a u t h o r ” m a x l e n g t h=” 100 ” /></p> <p> a b e l f o r=” i d e m a i l ”>Your e−m a i l :</ l a b e l> <l <i n p u t i d=” i d e m a i l ” t y p e=” t e x t ” name=” e m a i l ” m a x l e n g t h=” 75 ” /> </p> <p> a b e l f o r=” i d b o d y ”>Comment :</ l a b e l> <l <t e x t a r e a i d=” i d b o d y ” rows=” 10 ” c o l s=” 40 ” name=” body ”> t e x t a r e a> </ </p> 29 / 34
  • 23. What is Django? Contributed apps Features The Template Language References Simple Form Processing Form data validation Everything is done in Python, so it’s easily re-usable. c l a s s CommentForm ( f o r m s . Form ) : # ... def c l e a n e m a i l ( s e l f ) : e m a i l = s e l f . c l e a n e d d a t a [ ” e m a i l ” ] . s p l i t ( ”@” ) [ 1 ] i f e m a i l i n [ ” h o t m a i l . com” , ” yahoo . com” ] : r a i s e V a l i d a t i o n E r r o r , ” Get a GMail a c c o u n t ! ” 31 / 34
  • 24. What is Django? Features References “Django project site.” [Online]. Available: http://www.djangoproject.com/ 32 / 34
  • 25. What is Django? Features References Thank you for your attention. Any last question? 33 / 34
  • 26. What is Django? Features References Want to get some action? If you liked what you saw, we could meet somewhere and build a little website together :-) 34 / 34