Introduction to Django
    Master en Software Libre Caixanova

              May 22nd 2009
whoami


●   Portuguese since 1985
●   GTK+ developer
●   Proud Pythonista
●   Djangonaut since 2007
●   Igalian since 200...
My Latest Django Project




               Rancho
http://www.getrancho.com




   MSWL Caixanova · Vigo · May 22nd 2009 |...
What's Django




MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
What's Django?



quot;Django is a high-level Python Web framework
that encourages rapid development and clean,
pragmatic ...
Young But Strong



●   Internal project of Lawrence Journal-World in
    2003
●   Should help journalists meet fast deadl...
The Framework


●   Object-Relational Mapper
●   Automatic Admin Interface
●   Elegant URL Design
●   Powerful Template Sy...
MTV




Model-Template-View

●   Model: What things are
●   Templates: How things are presented
●   Views: How things are ...
Deployment



●   FastCGI
●   mod_python
●   mod_wsgi
●   ...



                 MSWL Caixanova · Vigo · May 22nd 2009 | ...
DB Backend



●   PostgreSQL
●   MySQL
●   SQLite
●   Oracle



                 MSWL Caixanova · Vigo · May 22nd 2009 | J...
Big Community



●   Django People:
     –    http://djangopeople.net
●   Django Pluggables:
     –    http://djangoplugga...
Using Django




MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Installation


Just get a tarball release or checkout the sources:
   http://www.djangoproject.com/download/


Then:
   # ...
Development




Django Projects have applications




        MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jroc...
Project



$ django-admin.py startproject Project

               Project/
                 __init__.py
                 m...
Project


Does it work?


                $ ./manage.py runserver




                MSWL Caixanova · Vigo · May 22nd 200...
Project




MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Applications

Apps are the project's components

          $ ./manage.py startapp recipe

  recipe/
     __init__.py
     ...
Configuration




        settings.py
      Easy configuration




MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha |...
Building The Database




$ ./manage.py syncdb



  MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia....
Models

models.py, series of classes describing objects




Represent the database objects.
Never touch SQL again!




   ...
Models



class Post(models.Model):
   title = models.CharField(max_length = 500)
   content = models.TextField()
   date ...
Views

views.py, series of functions that will normally process some models and render HTML




                          ...
Views

import datetime


def view_latest_posts(request):
    # Last 5 days
    date = datetime.datetime.now() -
          ...
Template




 Will let you not repeat yourself!

Will save designers from the code.




       MSWL Caixanova · Vigo · May...
Template


<html>
  <head>
    <title>{% block title %}{% endblock %}</title>
  </head>
  <body>
    {% block content %}{%...
Template

{% extends quot;base.htmlquot; %}

{% block title %}Homepage{% endblock %}

{% block content %}
  <h3>This will ...
URLs




In Django, URLs are part of the
            design!



       MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Roc...
URLs

urls.py use regular expressions to match URLs with views


urlpatterns = patterns('Project.some_app.views',
    (r'^...
Forms

forms.py, series of classes that represent an HTML form




Will let you easily configure the expected type of the
...
Forms


class CreatePost(forms.Form):
  title = forms.CharField(label = quot;Post Titlequot;,
                           m...
Forms

def create_post(request):
  if request.method == 'POST':
    form = CreatePost(request.POST)
    if form.is_valid()...
Forms


The quick way...


<form action=quot;/create/quot; method=quot;POSTquot;>
  {{ form.as_p }}
  <input type=quot;sub...
Performance




MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Performance




For those who doubt...

http://www.alrond.com/en/2007/jan/25/performance-test-of-6-leading-
frameworks/


...
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
HELP!




MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
HELP!

Django Docs

           http://docs.djangoproject.com

Some books

●   Learning Website Development with Django,
  ...
End




  Thank you!
       Questions?




MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Upcoming SlideShare
Loading in...5
×

Django Intro

1,942

Published on

An Introduction to Django given at the Master en Software Libre Caixanova.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,942
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
59
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Django Intro

  1. 1. Introduction to Django Master en Software Libre Caixanova May 22nd 2009
  2. 2. whoami ● Portuguese since 1985 ● GTK+ developer ● Proud Pythonista ● Djangonaut since 2007 ● Igalian since 2008 And if you insist... http://www.joaquimrocha.com MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  3. 3. My Latest Django Project Rancho http://www.getrancho.com MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  4. 4. What's Django MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  5. 5. What's Django? quot;Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.quot; From Django official website MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  6. 6. Young But Strong ● Internal project of Lawrence Journal-World in 2003 ● Should help journalists meet fast deadlines ● Should not stand in the journalists' way ● Got its name after the famous guitarrist Django Reinhardt MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  7. 7. The Framework ● Object-Relational Mapper ● Automatic Admin Interface ● Elegant URL Design ● Powerful Template System ● i18n it's amazing...! MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  8. 8. MTV Model-Template-View ● Model: What things are ● Templates: How things are presented ● Views: How things are processed MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  9. 9. Deployment ● FastCGI ● mod_python ● mod_wsgi ● ... MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  10. 10. DB Backend ● PostgreSQL ● MySQL ● SQLite ● Oracle MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  11. 11. Big Community ● Django People: – http://djangopeople.net ● Django Pluggables: – http://djangopluggables.com ● Django Sites: – http://www.djangosites.org ● ... MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  12. 12. Using Django MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  13. 13. Installation Just get a tarball release or checkout the sources: http://www.djangoproject.com/download/ Then: # python setup.py install ... yeah, that it! MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  14. 14. Development Django Projects have applications MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  15. 15. Project $ django-admin.py startproject Project Project/ __init__.py manage.py settings.py urls.py MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  16. 16. Project Does it work? $ ./manage.py runserver MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  17. 17. Project MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  18. 18. Applications Apps are the project's components $ ./manage.py startapp recipe recipe/ __init__.py models.py tests.py views.py MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  19. 19. Configuration settings.py Easy configuration MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  20. 20. Building The Database $ ./manage.py syncdb MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  21. 21. Models models.py, series of classes describing objects Represent the database objects. Never touch SQL again! MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  22. 22. Models class Post(models.Model): title = models.CharField(max_length = 500) content = models.TextField() date = models.DateTimeField(auto_now = True) ... MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  23. 23. Views views.py, series of functions that will normally process some models and render HTML Where the magic happen! How to get all blog posts from the latest 5 days and order them by descending date? MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  24. 24. Views import datetime def view_latest_posts(request): # Last 5 days date = datetime.datetime.now() - datetime.timedelta(5) posts = Post.objects.filter(date__gte = date).order_by('-date') return render_to_response('posts/show_posts.html', {'posts': posts}) MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  25. 25. Template Will let you not repeat yourself! Will save designers from the code. MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  26. 26. Template <html> <head> <title>{% block title %}{% endblock %}</title> </head> <body> {% block content %}{% endblock %} </body> </html> MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  27. 27. Template {% extends quot;base.htmlquot; %} {% block title %}Homepage{% endblock %} {% block content %} <h3>This will be some main content</h3> {% for post in posts %} <h4>{{ post.title }} on {{ post.date|date:quot;B d, Yquot;|upper }}<h4> <p>{{ post.content }}</p> {% endfor %} {% url project.some_app.views.some_view some arguments %} {% endblock %} MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  28. 28. URLs In Django, URLs are part of the design! MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  29. 29. URLs urls.py use regular expressions to match URLs with views urlpatterns = patterns('Project.some_app.views', (r'^$', 'index'), (r'^posts/(?P<r_id>d+)/$', 'view_latest_posts'), (r'^create/$', 'create'), ) MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  30. 30. Forms forms.py, series of classes that represent an HTML form Will let you easily configure the expected type of the inputs, error messages, labels, etc... MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  31. 31. Forms class CreatePost(forms.Form): title = forms.CharField(label = quot;Post Titlequot;, max_length = 500, widget = forms.TextInput(attrs={ 'class': 'big_entry' })) content = forms.CharField() tags = forms.CharField(required = False) MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  32. 32. Forms def create_post(request): if request.method == 'POST': form = CreatePost(request.POST) if form.is_valid(): # Create a new post object with data # from form.cleaned_data return HttpResponseRedirect('/index/') else: form = CreatePost() return render_to_response('create.html', { 'form': form, }) MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  33. 33. Forms The quick way... <form action=quot;/create/quot; method=quot;POSTquot;> {{ form.as_p }} <input type=quot;submitquot; value=quot;Createquot; /> </form> MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  34. 34. Performance MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  35. 35. Performance For those who doubt... http://www.alrond.com/en/2007/jan/25/performance-test-of-6-leading- frameworks/ MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  36. 36. MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  37. 37. HELP! MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  38. 38. HELP! Django Docs http://docs.djangoproject.com Some books ● Learning Website Development with Django, Packt ● Practical Django Projects, Apress ● Pro Django, Apress MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  39. 39. End Thank you! Questions? MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×