Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Django for Beginners

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

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Be the first to comment

Django for Beginners

  1. 1. An Introduction <ul><ul><li>Jason Davies </li></ul></ul><ul><ul><li>PyCon UK 2007 </li></ul></ul>
  2. 2. Django Reinhardt
  3. 3. ljworld.com
  4. 5. www.djangoproject.com
  5. 9. Overview of this Tutorial <ul><ul><li>Brief introduction and philosophy </li></ul></ul><ul><ul><li>Creating Django models </li></ul></ul><ul><ul><li>The automatic admin interface </li></ul></ul><ul><ul><li>Views (and URLs) </li></ul></ul><ul><ul><li>Templates </li></ul></ul><ul><ul><li>Comparison with other frameworks </li></ul></ul>
  6. 10. Django's Mission Statement <ul><ul><li>“Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.” </li></ul></ul>
  7. 11. Django Requirements <ul><li>Python (2.3+) </li></ul><ul><li>PostgreSQL / MySQL / SQLite / ... </li></ul><ul><li>Apache + mod_python / FastCGI / ... </li></ul>
  8. 12. “Projects” $ django-admin.py startproject myproject
  9. 13. myproject/ __init__.py manage.py settings.py urls.py
  10. 14. $ ./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.
  11. 16. “Apps” $ django-admin.py startapp blog
  12. 17. myproject/ blog/ __init__.py models.py views.py __init__.py manage.py settings.py urls.py
  13. 18. 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()
  14. 19. Activating Models $ ./manage.py syncdb Creating table blog_blog Creating table blog_post Loading 'initial_data' fixtures... No fixtures found.
  15. 20. 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']
  16. 23. Model API $ ./manage.py shell >>> from myproject.blog import Blog >>> b = Blog( ... title=”Jason's Fantastic Blog!!!”) >>> b.save()
  17. 24. >>> 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!!!
  18. 25. URLs ROOT_URLCONF = 'myproject.urls'
  19. 26. 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), )
  20. 27. Views from django.http import HttpResponse def post_list(request): return HttpReponse(“This is a list of posts!”)
  21. 28. 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...
  22. 29. 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!
  23. 30. 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...
  24. 31. 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>
  25. 32. The magic dot <ul><li>p[“name”] </li></ul><ul><li>p.name </li></ul><ul><li>p.name() </li></ul>
  26. 33. Filters {{ var|escape|linebreaks|... }}
  27. 34. 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>
  28. 35. {% 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 %}
  29. 36. Ruby on Rails http://www.rubyonrails.org/
  30. 37. http://www.alrond.com/en/2007/jan/25/performance-test-of-6-leading-frameworks/
  31. 38. Thank you for listening. Jason Davies [email_address] http://www.jasondavies.com/
  32. 39. http://www.mercurytide.co.uk/whitepapers/django-cheat-sheet/

    Be the first to comment

    Login to see the comments

  • newitfarmer

    Jan. 30, 2011
  • jaxiinofea

    Apr. 6, 2011
  • miracsa

    Apr. 8, 2011
  • reicky

    May. 28, 2012
  • juishanliang

    Feb. 21, 2013
  • cadu_leite

    Jun. 17, 2013
  • iandkenzt1

    Feb. 28, 2014
  • michaelpops

    Jul. 8, 2014
  • hhao99

    Mar. 31, 2015
  • MarianneLandry

    Apr. 27, 2015
  • SaiedDelshad

    Jul. 4, 2015
  • MaruthiPrithivirajan

    Jul. 15, 2015
  • autoaim

    Dec. 1, 2015
  • ssuser2de1ea

    Apr. 4, 2016
  • NyeinsuMon

    Jan. 16, 2017
  • fridahdafri

    Mar. 16, 2017
  • BebetoNyamwamu

    Apr. 9, 2017
  • shotasawaguchi

    Aug. 31, 2017
  • StevenRobertBrown

    Jun. 25, 2018
  • Shintyaaa

    Nov. 19, 2020

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

Views

Total views

24,603

On Slideshare

0

From embeds

0

Number of embeds

585

Actions

Downloads

861

Shares

0

Comments

0

Likes

36

×