Django for Beginners
Upcoming SlideShare
Loading in...5
×
 

Django for Beginners

on

  • 19,957 views

Slides for the "Django for Beginners" tutorial given at PyConUK 2007 by Jason Davies

Slides for the "Django for Beginners" tutorial given at PyConUK 2007 by Jason Davies

Statistics

Views

Total Views
19,957
Views on SlideShare
19,496
Embed Views
461

Actions

Likes
24
Downloads
429
Comments
1

14 Embeds 461

http://www.coderholic.com 210
http://www.jasondavies.com 77
http://markeev.labwr.ru 69
http://www.slideshare.net 61
http://www.dowling.me.uk 16
http://dschool.co 8
http://www.python.rk.edu.pl 8
http://www.rkblog.rk.edu.pl 4
http://localhost 3
http://127.0.0.1 1
http://translate.yandex.net 1
http://localhost:3000 1
http://web.archive.org 1
http://www.dschool.co 1
More...

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • REFERENCING SLIDESHARE GROUP 'KINGDOM OF BELGIUM' (Django Reinhardt)
    This group 'KINGDOM OF BELGIUM' aims to gather all people worldwide who love the Belgium, and their presentations. It is dedicated to ALL aspects of Belgium, without exception. Its history, its geography, its culture, its politic, its economy,tourism, literature, its varieties, Song, art, cinema, theater, etc.. In short all Sound slideshows or not (documents, simple slideshow, slidecast, youtube, video)... I welcome you! Join us! Thank you in advance for your support.

    http://www.slideshare.net/group/kingdom-of-belgium
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Hello! My name is Jason Davies; I'm a freelance Web developer from Cambridge and I've been using Django for about 2 years ever since it was open-sourced in July 2005. Hopefully this will give you a good introduction to the basics of Django. Simon Willison will cover even more stuff in the advanced tutorial.

Django for Beginners Django for Beginners Presentation Transcript

  • An Introduction
      • Jason Davies
      • PyCon UK 2007
  • Django Reinhardt
  • ljworld.com
  •  
  • www.djangoproject.com
  •  
  •  
  •  
  • Overview of this Tutorial
      • Brief introduction and philosophy
      • Creating Django models
      • The automatic admin interface
      • Views (and URLs)
      • Templates
      • Comparison with other frameworks
  • Django's Mission Statement
      • “Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.”
  • Django Requirements
    • Python (2.3+)
    • PostgreSQL / MySQL / SQLite / ...
    • Apache + mod_python / FastCGI / ...
  • “Projects” $ django-admin.py startproject myproject
  • myproject/ __init__.py manage.py settings.py urls.py
  • $ ./manage.py runserver Validating models... 0 errors found. Django version 0.96-pre, using settings 'myproject.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
  •  
  • “Apps” $ django-admin.py startapp blog
  • myproject/ blog/ __init__.py models.py views.py __init__.py manage.py settings.py urls.py
  • Creating Models from django.db import models class Blog(models.Model): title = models.CharField(maxlength=200) class Post(models.Model): title = models.CharField(maxlength=200) body = models.TextField() blog = models.ForeignKey(Blog) pub_date = models.DateTimeField()
  • Activating Models $ ./manage.py syncdb Creating table blog_blog Creating table blog_post Loading 'initial_data' fixtures... No fixtures found.
  • Activating the Admin Interface from django.db import models class Blog(models.Model): title = models.CharField(maxlength=200) class Admin: list_display = ['title'] class Post(models.Model): title = models.CharField(maxlength=200) body = models.TextField() blog = models.ForeignKey(Blog) pub_date = models.DateTimeField() class Admin: list_display = ['title', 'pub_date']
  •  
  •  
  • Model API $ ./manage.py shell >>> from myproject.blog import Blog >>> b = Blog( ... title=”Jason's Fantastic Blog!!!”) >>> b.save()
  • >>> all_blogs = Blog.objects.all() >>> print all_blogs [<Blog: Blog object>] >>> print all_blogs.name Jason's Fantastic Blog!!! >>> b = Blog.objects.get(name__contains='Jason') >>> print b.title Jason's Fantastic Blog!!!
  • URLs ROOT_URLCONF = 'myproject.urls'
  • URLconfs from django.conf.urls.defaults import * from myproject.blog.views import * urlpatterns = patterns('', (r'^admin/', include('django.contrib.admin.urls')), (r'^blog/$', post_list), (r'^blog/(?P<id>d+)/$', post_list), )
  • Views from django.http import HttpResponse def post_list(request): return HttpReponse(“This is a list of posts!”)
  • from django.http import HttpResponse from myproject.blog.models import Post def post_list(request): r = “<ul>” posts = Post.objects.order_by(“-pub_date”) for post in posts: r += “<li>%s: %s</li>” % (post.title, post.body) r += “</ul>” return HttpResponse(r) More realistic...
  • from django.shorcuts import render_to_response from myproject.blog.models import Post def post_list(request): posts = Post.objects.order_by(“-pub_date”) return render_to_response('blog/post_list.html', { 'post_list': posts, }) Better!
  • from django.shorcuts import render_to_response from myproject.blog.models import Post def post_detail(request, id): post = get_object_or_404(Post, id=id) return render_to_response('blog/post_detail.html', { 'post': post, }) For completeness...
  • Templates <html> <body> <h1>Jason's Fantastic Blog!!!</h1> <ul> {% for p in post_list %} <li> <a href=”{{ p.id }}/”>{{ p.title|escape }}</a> </li> {% endfor %} </ul> </body> </html>
  • The magic dot
    • p[“name”]
    • p.name
    • p.name()
  • Filters {{ var|escape|linebreaks|... }}
  • base.html <html> <head> <title>{% block title %}{% endblock %} </head> <body> <div id=”content”> {% block content %}{% endblock %} </div> <div id=”footer”> {% block footer %} Copyright Jason Davies 2007. {% endblock %} </div> </body> </html>
  • {% extends “base.html” %} {% block title %} Posts | {{ block.super }} {% endblock %} {% block content %} <h1>Blog Posts ({{ post_list|length }} total)</h1> <ul> {% for post in post_list %} <li> <a href=”{{ post.id }}/”> {{ post.title|escape }} </a> </li> {% endfor %} </ul> {% endblock %}
  • Ruby on Rails http://www.rubyonrails.org/
  • http://www.alrond.com/en/2007/jan/25/performance-test-of-6-leading-frameworks/
  • Thank you for listening. Jason Davies [email_address] http://www.jasondavies.com/
  • http://www.mercurytide.co.uk/whitepapers/django-cheat-sheet/
  •  
  •