SlideShare uma empresa Scribd logo
1 de 48
Python im Web




Python im Web: Django - Florian Herlings, 21.10.2009               1/48
Python im Web




Python im Web: Django - Florian Herlings, 21.10.2009               2/48
zuerst:




Python im Web: Django - Florian Herlings, 21.10.2009   3/48
Python ist eine Programmiersprache, die mehrere
                      Programmierparadigmen ermöglicht. So werden
                      objektorientierte, aspektorientierte und funktionale
                      Programmierung unterstützt.

                                                       http://de.wikipedia.org/wiki/Python_(Programmiersprache)




Python im Web: Django - Florian Herlings, 21.10.2009                                                              4/48
schwach, dynamisch typisierte


                      Python ist eine Programmiersprache, die mehrere
                      Programmierparadigmen ermöglicht. So werden
                      objektorientierte, aspektorientierte und funktionale
                      Programmierung unterstützt.

                                                         http://de.wikipedia.org/wiki/Python_(Programmiersprache)




Python im Web: Django - Florian Herlings, 21.10.2009                                                                5/48
Python ist eine Programmiersprache, die mehrere
                      Programmierparadigmen ermöglicht. So werden
                      objektorientierte, aspektorientierte und funktionale
                      Programmierung unterstützt.                               Erfunden von einem Holländer

                                                       http://de.wikipedia.org/wiki/Python_(Programmiersprache)




Python im Web: Django - Florian Herlings, 21.10.2009                                                              6/48
Guido


                                                          http://www.flickr.com/photos/47178427@N00/176564536/   Lizenz: CC BY 2.0

Python im Web: Django - Florian Herlings, 21.10.2009                                                                        7/48
Guido van Rossum
                                                                           „Vor über sechs Jahren, im Dezember 1989, suchte
                                                                           ich nach einem Programmierprojekt, das mich über
                                                                           die Weihnachtswoche beschäftigen würde.
                                                                           Mein Büro würde geschlossen bleiben, aber ich
                                                                           hatte auch zu Hause einen PC und sonst nicht viel zu
                                                                           tun.
                                                                           Ich entschied mich, einen Interpreter für die
                                                                           Scriptsprache zu schreiben, über die ich kürzlich
                                                                           nachdachte: Ein Nachfolger von ABC, der auch Unix-
                                                                           und C-Hacker ansprechen würde. Ich wählte Python
                                                                           als Arbeitstitel für das Projekt, weil ich in einer
                                                                           leicht respektlosen Stimmung (und ein großer Fan
                                                                           des Monty Python’s Flying Circus) war.“

    http://www.flickr.com/photos/docsearls/        Lizenz: CC BY-SA 2.0


Python im Web: Django - Florian Herlings, 21.10.2009                                                                              8/48
Python im Web: Django - Florian Herlings, 21.10.2009   9/48
The Zen of Python
       Beautiful is better than ugly.
       Explicit is better than implicit.
       Simple is better than complex.
       Complex is better than complicated.
       Flat is better than nested.
       Sparse is better than dense.
       Readability counts.
       Special cases aren't special enough to break the rules.
       Although practicality beats purity.
       Errors should never pass silently.
       Unless explicitly silenced.
       In the face of ambiguity, refuse the temptation to guess.
       There should be one-- and preferably only one --obvious way to do it.
       Although that way may not be obvious at first unless you're Dutch.
       Now is better than never.
       Although never is often better than *right* now.
       If the implementation is hard to explain, it's a bad idea.
       If the implementation is easy to explain, it may be a good idea.
       Namespaces are one honking great idea -- let's do more of those!


Python im Web: Django - Florian Herlings, 21.10.2009                           10/48
Python-Syntax
                     - dynamisch typisierte Skriptsprache
                     - wenig verbose Syntax (hier Java-Scherz einfügen ;)
                     - „batteries included“ (sehr große Standard-Library)
                     - für jedes Problem soll es einen und zwar genau
                        einen Weg geben es in der Sprache zu lösen
                     - Alles ist ein Objekt (ja, auch Funktionen!)
                     - Einrückung statt geschweifter Klammern (WTF?)



Python im Web: Django - Florian Herlings, 21.10.2009                        11/48
Verschachtelung

                         if (name == 'Peter'):
                             print 'Hallo Peter.'

                         else:
                             print 'Hallo Du!'




Python im Web: Django - Florian Herlings, 21.10.2009   12/48
Verschachtelung

                         if (name == 'Peter'):
                             print 'Hallo Peter.'

                         else:
                             print 'Hallo Du!'

                     4 Leerzeichen, 2 Leerzeichen oder ein Tab




Python im Web: Django - Florian Herlings, 21.10.2009             13/48
Arrays

                 autos = ['Mercedes', 'BMW', 'Opel']

                 for auto in autos:
                     print 'Auto: ' + auto


                                                       Ausgabe:
                                                         Auto: Mercedes
                                                         Auto: BMW
                                                         Auto: Opel




Python im Web: Django - Florian Herlings, 21.10.2009                      14/48
Arrays: list comprehension

                 autos = ['Mercedes', 'BMW', 'Opel']

                 kleine_autos = [auto.lower() for auto in autos]

                   ['mercedes', 'bmw', 'opel']




Python im Web: Django - Florian Herlings, 21.10.2009               15/48
Arrays: list comprehension

                  autos = ['Mercedes', 'BMW', 'Opel']

                  teure_autos = [auto.lower()
                                 for auto in autos
                                 if auto != 'Opel']

          ['mercedes', 'bmw']




Python im Web: Django - Florian Herlings, 21.10.2009    16/48
Dictionaries

                  auto = {'name': 'Kapitän', 'hersteller': 'Opel'}

                  print auto['name']


                                                       Ausgabe:
                                                         Kapitän




Python im Web: Django - Florian Herlings, 21.10.2009                 17/48
Funktionen

                 def sag_hallo(name):
                     print 'Hallo, wie geht es dir %s?' % name


                 sag_hallo('Peter')
                 sag_hallo(name = 'Karl-Heinz')



                                                       Ausgabe:
                                                         Hallo, wie geht es dir Peter?
                                                         Hallo, wie geht es dir Karl-Heinz?

Python im Web: Django - Florian Herlings, 21.10.2009                                          18/48
Klassen
                    class Auto(object):

                               def __init__(self):
                                   print 'Guten Morgen!'

                               def sag_hallo(self):
                                   print 'Hallo, wie geht es dir?'


                    mein_auto = Auto()
                    mein_auto.sag_hallo()




Python im Web: Django - Florian Herlings, 21.10.2009                 19/48
Klassen
                    class Auto(object):

                               def __init__(self):
                                   print 'Guten Morgen!'
                                                       __init__ = Konstruktor
                               def sag_hallo(self):
                                   print 'Hallo, wie geht es dir?'


                    mein_auto = Auto()
                    mein_auto.sag_hallo()




Python im Web: Django - Florian Herlings, 21.10.2009                            20/48
Klassen
                    class Auto(object):                Referenz auf das aktuelle Objekt


                               def __init__(self):
                                   print 'Guten Morgen!'

                               def sag_hallo(self):
                                   print 'Hallo, wie geht es dir?'


                    mein_auto = Auto()
                    mein_auto.sag_hallo()




Python im Web: Django - Florian Herlings, 21.10.2009                                      21/48
Klassen
                    class Auto(object):

                               def __init__(self):
                                   print 'Guten Morgen!'

                               def sag_hallo(self):
                                   print 'Hallo, wie geht es dir?'


                    mein_auto = Auto()                 Ausgabe:
                    mein_auto.sag_hallo()                Guten Morgen
                                                         Hallo, wie geht es dir?


Python im Web: Django - Florian Herlings, 21.10.2009                               22/48
Das reicht erstmal!
                          Aber es gibt noch viel mehr auf python.org




Python im Web: Django - Florian Herlings, 21.10.2009                   23/48
Eigentlich ging es ja um...



Python im Web: Django - Florian Herlings, 21.10.2009   24/48
Python im Web: Django - Florian Herlings, 21.10.2009   25/48
The Web framework for perfectionists with deadlines.




Python im Web: Django - Florian Herlings, 21.10.2009                       26/48
The Web framework for perfectionists with deadlines.
                                                       die coolste Punchline aller Zeiten




Python im Web: Django - Florian Herlings, 21.10.2009                                        27/48
Django:
                     - Web(2.0)-Framework
                     - 100% Buzzword-kompatibel
                     - MVC (oder so ähnlich)
                     - ORM
                     - viele, kleine Applications (Blog, Backend, User-Verwaltung, ...)
                     - Backend-Generator
                     - Schnell (sehr schnell)
                     - BSD-Lizenz (wie die meisten Python-Projekte)

Python im Web: Django - Florian Herlings, 21.10.2009                                      28/48
Django-Projekt-Struktur



                        blog/                          Eine Applikation
                        __init__.py
                        settings.py                    Einstellungen
                        manage.py                      Kommandozeilen-Tool
                        urls.py                        Routing




Python im Web: Django - Florian Herlings, 21.10.2009                         29/48
Django-Projekt-Struktur



                        blog/                          templates/    Template-Dateien(HTML)
                        __init__.py                    __init__.py
                        settings.py                    models.py     Models
                        manage.py                      views.py      Views
                        urls.py




Python im Web: Django - Florian Herlings, 21.10.2009                                          30/48
MVC
                                                             Model, View, Controller




Python im Web: Django - Florian Herlings, 21.10.2009                                   31/48
MVC

                                                       MVT   Model, View, Template




Python im Web: Django - Florian Herlings, 21.10.2009                                 32/48
Model

                     from django.db import models



                     class BlogEntry(models.Model):
                              title = models.CharField('Titel', max_length=128)
                              content = models.TextField('Inhalt')
                              author = models.EmailField('E-Mail-Adresse')
                              last_change = models.DateTimeField(auto_now = True)




Python im Web: Django - Florian Herlings, 21.10.2009                                33/48
Model
                     from django.contrib import admin
                     from django.db import models



                     class BlogEntry(models.Model):
                              title = models.CharField('Titel', max_length=128)
                              content = models.TextField('Inhalt')
                              author = models.EmailField('E-Mail-Adresse')
                              last_change = models.DateTimeField(auto_now = True)


                     admin.site.register(BlogEntry)




Python im Web: Django - Florian Herlings, 21.10.2009                                34/48
Views
                     from django.shortcuts import HttpResponse, render_to_response
                     from models import *



                     def dummy(request):
                              return HttpResponse('Hallo, ich bin ein Dummy.')


                     def all_blog_entries(request):
                              data = {'entries': BlogEntry.objects.all() }
                              return render_to_response('list.html', data)




Python im Web: Django - Florian Herlings, 21.10.2009                                 35/48
Template (list.html)
                     {% extends 'layout.html' %}
                     {% block content %}
                        <h2>Alle Blog-Eintr&auml;ge</h2>
                                                             Über die Blog-Einträge iterieren
                        {% for entry in entries %}
                            <div style="border: solid silver 1px;">
                                <h3>{{ entry.title }} von {{ entry.author }}</h3>
                                <p>{{ entry.content }}</p>
                            </div>
                        {% endfor %}
                     {% endblock %}




Python im Web: Django - Florian Herlings, 21.10.2009                                            36/48
Template (list.html)
                     {% extends 'layout.html' %}
                     {% block content %}
                        <h2>Alle Blog-Eintr&auml;ge</h2>
                        {% for entry in entries %}
                            <div style="border: solid silver 1px;">
                                <h3>{{ entry.title }} von {{ entry.author }}</h3>
                                <p>{{ entry.content }}</p>
                            </div>
                                                                      Inhalte ausgeben

                        {% endfor %}
                     {% endblock %}




Python im Web: Django - Florian Herlings, 21.10.2009                                     37/48
Template (list.html)
                     {% extends 'layout.html' %}
                     {% block content %}
                        <h2>Alle Blog-Eintr&auml;ge</h2>
                        {% for entry in entries %}
                            <div style="border: solid silver 1px;">
                                <h3>{{ entry.title }} von {{ entry.author }}</h3>
                                <p>{{ entry.content }}</p>
                            </div>
                        {% endfor %}
                     {% endblock %}




Python im Web: Django - Florian Herlings, 21.10.2009                                38/48
Template (layout.html)
                     <html xmlns="http://www.w3.org/1999/xhtml">
                        <head>[...]</head>
                     <body>
                            {% block content %}
                                <!-- DEFAULT CONTENT -->
                            {% endblock %}
                     </body>
                     </html>




Python im Web: Django - Florian Herlings, 21.10.2009               39/48
Routing (urls.py)
                     from django.conf.urls.defaults import *
                     from django.contrib import admin
                     admin.autodiscover()


                     urlpatterns = patterns (
                           url( r'^$', 'fb5dev.blog.views.dummy', name='dummy_page' ),
                           url( r'^list$', 'fb5dev.blog.views.list_blog_entries', name='list_blog_entries' ),
                           url(r'^admin/', include(admin.site.urls)),
                     )




Python im Web: Django - Florian Herlings, 21.10.2009                                                            40/48
Demo



Python im Web: Django - Florian Herlings, 21.10.2009          41/48
Aus Zeitgründen ausgelassen
                     - jede Menge HTML-Helper
                     - fortgeschrittenes Routing
                     - automatisches Generieren von Formularen
                     - eingebaute User-Verwaltung
                     - eingebautes I18N/L10N




Python im Web: Django - Florian Herlings, 21.10.2009             42/48
Mehr auf
                                      djangoproject.com


Python im Web: Django - Florian Herlings, 21.10.2009      43/48
Eine Sammlung von bereits erstellten, generischen Apps,
                   die sich nahtlos in eine bestehende Django-Projekte
                   einbetten lassen.


                   Projekt-Seite: pinaxproject.com




Python im Web: Django - Florian Herlings, 21.10.2009                         44/48
Features
    openid support
    email verification
    password management
    site announcements
    a notification framework
    user-to-user messaging
    friend invitation (both internal and external to the site)
    a basic twitter clone
    oembed support
    gravatar support
    interest groups (called tribes)
    projects with basic task and issue management
    threaded discussions
    wikis with multiple markup support
    blogging
    bookmarks
    tagging
    contact import (from vCard, Google or Yahoo)
    photo management


Python im Web: Django - Florian Herlings, 21.10.2009             45/48
Demo-Seite: cloud27.com




Python im Web: Django - Florian Herlings, 21.10.2009   46/48
Danke.
                                                         :)


Python im Web: Django - Florian Herlings, 21.10.2009            47/48
FlorianHerlings.de

                   Lizenz: Attribution-Noncommercial-Share Alike 3.0 Germany
                   http://creativecommons.org/licenses/by-nc-sa/3.0/de/




Python im Web: Django - Florian Herlings, 21.10.2009                           48/48

Mais conteúdo relacionado

Destaque

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destaque (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Python im Web: Django

  • 1. Python im Web Python im Web: Django - Florian Herlings, 21.10.2009 1/48
  • 2. Python im Web Python im Web: Django - Florian Herlings, 21.10.2009 2/48
  • 3. zuerst: Python im Web: Django - Florian Herlings, 21.10.2009 3/48
  • 4. Python ist eine Programmiersprache, die mehrere Programmierparadigmen ermöglicht. So werden objektorientierte, aspektorientierte und funktionale Programmierung unterstützt. http://de.wikipedia.org/wiki/Python_(Programmiersprache) Python im Web: Django - Florian Herlings, 21.10.2009 4/48
  • 5. schwach, dynamisch typisierte Python ist eine Programmiersprache, die mehrere Programmierparadigmen ermöglicht. So werden objektorientierte, aspektorientierte und funktionale Programmierung unterstützt. http://de.wikipedia.org/wiki/Python_(Programmiersprache) Python im Web: Django - Florian Herlings, 21.10.2009 5/48
  • 6. Python ist eine Programmiersprache, die mehrere Programmierparadigmen ermöglicht. So werden objektorientierte, aspektorientierte und funktionale Programmierung unterstützt. Erfunden von einem Holländer http://de.wikipedia.org/wiki/Python_(Programmiersprache) Python im Web: Django - Florian Herlings, 21.10.2009 6/48
  • 7. Guido http://www.flickr.com/photos/47178427@N00/176564536/ Lizenz: CC BY 2.0 Python im Web: Django - Florian Herlings, 21.10.2009 7/48
  • 8. Guido van Rossum „Vor über sechs Jahren, im Dezember 1989, suchte ich nach einem Programmierprojekt, das mich über die Weihnachtswoche beschäftigen würde. Mein Büro würde geschlossen bleiben, aber ich hatte auch zu Hause einen PC und sonst nicht viel zu tun. Ich entschied mich, einen Interpreter für die Scriptsprache zu schreiben, über die ich kürzlich nachdachte: Ein Nachfolger von ABC, der auch Unix- und C-Hacker ansprechen würde. Ich wählte Python als Arbeitstitel für das Projekt, weil ich in einer leicht respektlosen Stimmung (und ein großer Fan des Monty Python’s Flying Circus) war.“ http://www.flickr.com/photos/docsearls/ Lizenz: CC BY-SA 2.0 Python im Web: Django - Florian Herlings, 21.10.2009 8/48
  • 9. Python im Web: Django - Florian Herlings, 21.10.2009 9/48
  • 10. The Zen of Python Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! Python im Web: Django - Florian Herlings, 21.10.2009 10/48
  • 11. Python-Syntax - dynamisch typisierte Skriptsprache - wenig verbose Syntax (hier Java-Scherz einfügen ;) - „batteries included“ (sehr große Standard-Library) - für jedes Problem soll es einen und zwar genau einen Weg geben es in der Sprache zu lösen - Alles ist ein Objekt (ja, auch Funktionen!) - Einrückung statt geschweifter Klammern (WTF?) Python im Web: Django - Florian Herlings, 21.10.2009 11/48
  • 12. Verschachtelung if (name == 'Peter'): print 'Hallo Peter.' else: print 'Hallo Du!' Python im Web: Django - Florian Herlings, 21.10.2009 12/48
  • 13. Verschachtelung if (name == 'Peter'): print 'Hallo Peter.' else: print 'Hallo Du!' 4 Leerzeichen, 2 Leerzeichen oder ein Tab Python im Web: Django - Florian Herlings, 21.10.2009 13/48
  • 14. Arrays autos = ['Mercedes', 'BMW', 'Opel'] for auto in autos: print 'Auto: ' + auto Ausgabe: Auto: Mercedes Auto: BMW Auto: Opel Python im Web: Django - Florian Herlings, 21.10.2009 14/48
  • 15. Arrays: list comprehension autos = ['Mercedes', 'BMW', 'Opel'] kleine_autos = [auto.lower() for auto in autos] ['mercedes', 'bmw', 'opel'] Python im Web: Django - Florian Herlings, 21.10.2009 15/48
  • 16. Arrays: list comprehension autos = ['Mercedes', 'BMW', 'Opel'] teure_autos = [auto.lower() for auto in autos if auto != 'Opel'] ['mercedes', 'bmw'] Python im Web: Django - Florian Herlings, 21.10.2009 16/48
  • 17. Dictionaries auto = {'name': 'Kapitän', 'hersteller': 'Opel'} print auto['name'] Ausgabe: Kapitän Python im Web: Django - Florian Herlings, 21.10.2009 17/48
  • 18. Funktionen def sag_hallo(name): print 'Hallo, wie geht es dir %s?' % name sag_hallo('Peter') sag_hallo(name = 'Karl-Heinz') Ausgabe: Hallo, wie geht es dir Peter? Hallo, wie geht es dir Karl-Heinz? Python im Web: Django - Florian Herlings, 21.10.2009 18/48
  • 19. Klassen class Auto(object): def __init__(self): print 'Guten Morgen!' def sag_hallo(self): print 'Hallo, wie geht es dir?' mein_auto = Auto() mein_auto.sag_hallo() Python im Web: Django - Florian Herlings, 21.10.2009 19/48
  • 20. Klassen class Auto(object): def __init__(self): print 'Guten Morgen!' __init__ = Konstruktor def sag_hallo(self): print 'Hallo, wie geht es dir?' mein_auto = Auto() mein_auto.sag_hallo() Python im Web: Django - Florian Herlings, 21.10.2009 20/48
  • 21. Klassen class Auto(object): Referenz auf das aktuelle Objekt def __init__(self): print 'Guten Morgen!' def sag_hallo(self): print 'Hallo, wie geht es dir?' mein_auto = Auto() mein_auto.sag_hallo() Python im Web: Django - Florian Herlings, 21.10.2009 21/48
  • 22. Klassen class Auto(object): def __init__(self): print 'Guten Morgen!' def sag_hallo(self): print 'Hallo, wie geht es dir?' mein_auto = Auto() Ausgabe: mein_auto.sag_hallo() Guten Morgen Hallo, wie geht es dir? Python im Web: Django - Florian Herlings, 21.10.2009 22/48
  • 23. Das reicht erstmal! Aber es gibt noch viel mehr auf python.org Python im Web: Django - Florian Herlings, 21.10.2009 23/48
  • 24. Eigentlich ging es ja um... Python im Web: Django - Florian Herlings, 21.10.2009 24/48
  • 25. Python im Web: Django - Florian Herlings, 21.10.2009 25/48
  • 26. The Web framework for perfectionists with deadlines. Python im Web: Django - Florian Herlings, 21.10.2009 26/48
  • 27. The Web framework for perfectionists with deadlines. die coolste Punchline aller Zeiten Python im Web: Django - Florian Herlings, 21.10.2009 27/48
  • 28. Django: - Web(2.0)-Framework - 100% Buzzword-kompatibel - MVC (oder so ähnlich) - ORM - viele, kleine Applications (Blog, Backend, User-Verwaltung, ...) - Backend-Generator - Schnell (sehr schnell) - BSD-Lizenz (wie die meisten Python-Projekte) Python im Web: Django - Florian Herlings, 21.10.2009 28/48
  • 29. Django-Projekt-Struktur blog/ Eine Applikation __init__.py settings.py Einstellungen manage.py Kommandozeilen-Tool urls.py Routing Python im Web: Django - Florian Herlings, 21.10.2009 29/48
  • 30. Django-Projekt-Struktur blog/ templates/ Template-Dateien(HTML) __init__.py __init__.py settings.py models.py Models manage.py views.py Views urls.py Python im Web: Django - Florian Herlings, 21.10.2009 30/48
  • 31. MVC Model, View, Controller Python im Web: Django - Florian Herlings, 21.10.2009 31/48
  • 32. MVC MVT Model, View, Template Python im Web: Django - Florian Herlings, 21.10.2009 32/48
  • 33. Model from django.db import models class BlogEntry(models.Model): title = models.CharField('Titel', max_length=128) content = models.TextField('Inhalt') author = models.EmailField('E-Mail-Adresse') last_change = models.DateTimeField(auto_now = True) Python im Web: Django - Florian Herlings, 21.10.2009 33/48
  • 34. Model from django.contrib import admin from django.db import models class BlogEntry(models.Model): title = models.CharField('Titel', max_length=128) content = models.TextField('Inhalt') author = models.EmailField('E-Mail-Adresse') last_change = models.DateTimeField(auto_now = True) admin.site.register(BlogEntry) Python im Web: Django - Florian Herlings, 21.10.2009 34/48
  • 35. Views from django.shortcuts import HttpResponse, render_to_response from models import * def dummy(request): return HttpResponse('Hallo, ich bin ein Dummy.') def all_blog_entries(request): data = {'entries': BlogEntry.objects.all() } return render_to_response('list.html', data) Python im Web: Django - Florian Herlings, 21.10.2009 35/48
  • 36. Template (list.html) {% extends 'layout.html' %} {% block content %} <h2>Alle Blog-Eintr&auml;ge</h2> Über die Blog-Einträge iterieren {% for entry in entries %} <div style="border: solid silver 1px;"> <h3>{{ entry.title }} von {{ entry.author }}</h3> <p>{{ entry.content }}</p> </div> {% endfor %} {% endblock %} Python im Web: Django - Florian Herlings, 21.10.2009 36/48
  • 37. Template (list.html) {% extends 'layout.html' %} {% block content %} <h2>Alle Blog-Eintr&auml;ge</h2> {% for entry in entries %} <div style="border: solid silver 1px;"> <h3>{{ entry.title }} von {{ entry.author }}</h3> <p>{{ entry.content }}</p> </div> Inhalte ausgeben {% endfor %} {% endblock %} Python im Web: Django - Florian Herlings, 21.10.2009 37/48
  • 38. Template (list.html) {% extends 'layout.html' %} {% block content %} <h2>Alle Blog-Eintr&auml;ge</h2> {% for entry in entries %} <div style="border: solid silver 1px;"> <h3>{{ entry.title }} von {{ entry.author }}</h3> <p>{{ entry.content }}</p> </div> {% endfor %} {% endblock %} Python im Web: Django - Florian Herlings, 21.10.2009 38/48
  • 39. Template (layout.html) <html xmlns="http://www.w3.org/1999/xhtml"> <head>[...]</head> <body> {% block content %} <!-- DEFAULT CONTENT --> {% endblock %} </body> </html> Python im Web: Django - Florian Herlings, 21.10.2009 39/48
  • 40. Routing (urls.py) from django.conf.urls.defaults import * from django.contrib import admin admin.autodiscover() urlpatterns = patterns ( url( r'^$', 'fb5dev.blog.views.dummy', name='dummy_page' ), url( r'^list$', 'fb5dev.blog.views.list_blog_entries', name='list_blog_entries' ), url(r'^admin/', include(admin.site.urls)), ) Python im Web: Django - Florian Herlings, 21.10.2009 40/48
  • 41. Demo Python im Web: Django - Florian Herlings, 21.10.2009 41/48
  • 42. Aus Zeitgründen ausgelassen - jede Menge HTML-Helper - fortgeschrittenes Routing - automatisches Generieren von Formularen - eingebaute User-Verwaltung - eingebautes I18N/L10N Python im Web: Django - Florian Herlings, 21.10.2009 42/48
  • 43. Mehr auf djangoproject.com Python im Web: Django - Florian Herlings, 21.10.2009 43/48
  • 44. Eine Sammlung von bereits erstellten, generischen Apps, die sich nahtlos in eine bestehende Django-Projekte einbetten lassen. Projekt-Seite: pinaxproject.com Python im Web: Django - Florian Herlings, 21.10.2009 44/48
  • 45. Features openid support email verification password management site announcements a notification framework user-to-user messaging friend invitation (both internal and external to the site) a basic twitter clone oembed support gravatar support interest groups (called tribes) projects with basic task and issue management threaded discussions wikis with multiple markup support blogging bookmarks tagging contact import (from vCard, Google or Yahoo) photo management Python im Web: Django - Florian Herlings, 21.10.2009 45/48
  • 46. Demo-Seite: cloud27.com Python im Web: Django - Florian Herlings, 21.10.2009 46/48
  • 47. Danke. :) Python im Web: Django - Florian Herlings, 21.10.2009 47/48
  • 48. FlorianHerlings.de Lizenz: Attribution-Noncommercial-Share Alike 3.0 Germany http://creativecommons.org/licenses/by-nc-sa/3.0/de/ Python im Web: Django - Florian Herlings, 21.10.2009 48/48