Python & Django: Fast and easy web application development

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    19 Favorites

    Python & Django: Fast and easy web application development - Presentation Transcript

    1. Python & Django Fast and easy web application development 4developers'09 Marcin Mierzejewski // www.zenzire.com
    2. Agenda Python: Django: • features • history • examples • features • architecture • example
    3. Python Guido van Rossum, 1990 Interpreter Paradigms: • structural • object oriented • functional Readability
    4. Python. Open Source Platforms: • Windows • Linux • ... Implementation: CPython, JPython, IronPython, PyPy, Psyco
    5. Interpreter ~$ python >>> print 'hello 4developers' hello 4developers >>> text = 'Python & Djnago' >>> print text[0] P >>> print text[:6], text[-6:] Python Django >>> print text[9:11] * 3 DjDjDj
    6. Structural a = 10 if a > 5: print 'bigger' else: print 'lesser' for i in ['A', 'be', 3, 4.5, True]: print i
    7. Object oriented class A: def function_a(self): print 'A' class B(A): def function_b(self): self.function_a() print 'B' a = A() a.function_a() ----------> A b = B() b.function_b() ----------> A B
    8. Functional def factorial(x): if x == 0: return 1 else: return x * factorial(x-1) factorial(10) factorial = lambda x:(1 if x==0 else x * factorial(x-1)) factorial(10)
    9. Django Framework to easy and fast develop web application
    10. History 2003: Kansas, USA 2005: BSD & v0.90 2006: v0.95 2007: v0.96 2008: Fundation & v1.0 2009: EuroDjango
    11. Features Documentation HTTP Server ORM URL dispatcher Templates Interactive Console
    12. Features Administration Panel Forms Middleware & Sygnals Authentication & Authorization i18n i L10n Cache
    13. Architecture MVC (MTV ) Model Template View Model - data View - logic Template - presentation
    14. Architecture Web Browser Template URL View Model Database
    15. Model Web Browser Template URL View Model Database
    16. Model SQL Free ORM Relations API
    17. Model class Category(models.Model): name = models.CharField(max_length=200) slug = models.SlugField(unique=True) class Entry(models.Model): title = models.CharField(max_length=200) slug = models.SlugField(unique=True) content = models.TextField() data = models.DateTimeField(default=datetime.now) categories = models.ManyToManyField(Category)
    18. Model API >>> category = Category(slug='django', name='Django') >>> category.save() >>> print category.name U'Django' >>> categories = Category.objects.all() >>> categories = Category.objects.filter(slug='django') >>> categories [<Category: Category object>] >>> entry = Entry(slug='hello',title='Hello',content='') >>> entry.save() >>> entry.categories.add( categories[0] ) >>> print entry.categories.all() [<Category: Category object>]
    19. View Web Browser Template URL View Model Database
    20. View def entry_list(request): entries = Entry.objects.all()[:5] return render_to_response('list.html', \\ {'entries': entries}) def entry_details(request, slug): entry = get_object_or_404(Entry, slug = slug) return render_to_response('details.html', \\ {'entry': entry})
    21. Template Web Browser Template URL View Model Database
    22. Template {{ variables }}, {% tags %} i {{ variable|filter }} <html> <head><title>My blog</title></head> <body> {% for entry in entries %} <h1>{{ entry.title|upper }}</h1> {{ entry.content }}<br/> Published {{ entry.data|date:\"d F Y\" }}, <a href=”/{{ entry.slug }}/”>link</a>. {% endfor %} </body> </html>
    23. URL Web Browser Template URL View Model Database
    24. URL # http://mywebsite.com/4developers/ (r'^(?P<slug>[-\\w]+)/$', 'blog.views.entry_details'), # http://mywebsite.com/ (r'^$', 'blog.views.entry_list'),
    25. Architecture Web Browser Template URL View Model Database
    26. Example Entry List Entry Details Administrator Panel Users
    27. Project # django-admin.py startproject project project/ __init__.py manage.py settings.py [1] urls.py [2] [1] [2] DATABASE_ENGINE from django.contrib import admin DATABASE_NAME admin.autodiscover() INSTALLED_APPS = ( ... (r'^admin/(.*)', include('admin.site.root')) 'django.contrib.admin'
    28. Tables project# manage.py syncdb Creating table auth_permission Creating table auth_group Creating table auth_user Creating table auth_message Creating table django_content_type Creating table django_session Creating table django_site ......................... project# manage.py runserver Validating models... 0 errors found Django version 1.0-final-SVN-unknown, .......... Development server is running at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.
    29. Administration Panel http://127.0.0.1:8000/admin/
    30. Blog project# manage.py startapp blog project/ settings.py ← INSTALLED_APPS = ( ... ... 'blog',) blog/ __init__.py models.py views.py
    31. models.py project/blog/models.py class Entry(models.Model): title = models.CharField(max_length=200) slug = models.SlugField(unique=True) content = models.TextField() data = models.DateTimeField(default=datetime.now) def __unicode__(self): return u'%s' % self.title admin.site.register(Entry)
    32. models.py project# manage.py syncdb project# manage.py runserver
    33. views.py project/blog/views.py def entry_list(request): entries = Entry.objects.all().order_by('-data')[:5] return render_to_response('list.html', \\ {'entries': entries}) def entry_details(request, slug): entry = get_object_or_404(Entry, slug = slug) return render_to_response('details.html', \\ {'entry': entry})
    34. urls.py project/urls.py: (r'^(?P<slug>[-\\w]+)/$', 'blog.views.entry_details'), (r'^$', 'blog.views.entry_list'),
    35. Templates project/template/list.html <html> <body> {% for entry in entries%} <h1>{{ entry.title|upper }}</h1> {{ entry.content }}<br> Published {{ entry.data|date:\"d F Y\" }}<br/> <a href=\"/{{ entry.slug }}/\">Link</a> {% endfor %} </body> </html> project/template/details.html <html> <body> <h1>{{ entry.title|upper }}</h1> {{ entry.contentc }}<br> Published {{ entry.data|date:\"d F Y\" }}<br/> </body> </html>
    36. Done http://127.0.0.1:8000/ http://127.0.0.1:8000/4developers-2008
    37. Statistics views.py + models.py + settings.py + urls.py = 30 LoC list.html + details.html = 17 LoC
    38. Thank You For more details: http://www.djangoproject.com/
    39. Pictures http://www.flickr.net/photos/ecstaticist/1340787730/ http://www.flickr.net/photos/schoffer/196079076/ http://www.flickr.net/photos/kogakure/2225768345/ http://www.flickr.net/photos/hotsaucejane/2385963623/ http://www.flickr.net/photos/mag-cafe/163556150/ http://www.flickr.net/photos/pauliogeordio/2301563705/ http://www.flickr.net/photos/origomi/345911878/ http://www.flickr.net/photos/ravages/2831688538/ http://www.flickr.net/photos/dizzygirl/125328389/

    + Marcin MierzejewskiMarcin Mierzejewski, 8 months ago

    custom

    1946 views, 19 favs, 2 embeds more stats

    Presentation from 4Developers 2008 conference in Kr more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1946
      • 1847 on SlideShare
      • 99 from embeds
    • Comments 0
    • Favorites 19
    • Downloads 0
    Most viewed embeds
    • 88 views on http://www.zenzire.com
    • 11 views on http://praveensunsetpoint.wordpress.com

    more

    All embeds
    • 88 views on http://www.zenzire.com
    • 11 views on http://praveensunsetpoint.wordpress.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories