Django a whirlwind tour

811 views
715 views

Published on

A quick introduction to Django with tips on deploying to Heroku. Presented at TechCamp Memphis, Fall 2012

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

No Downloads
Views
Total views
811
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
13
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Django a whirlwind tour

  1. 1. Django: A Whirlwind Tour Brad Montgomery Email: brad@workforpie.com Twitter: bkmontgomeryFriday, November 2, 12
  2. 2. Django: Features • Python • ORM: Object-Relational Mapper • MVC-inspired (MVT) • Clean URLs • Huge Community • Worlds Best DocumentationFriday, November 2, 12
  3. 3. Batteries Included aka: contrib apps • admin • auth • comments • gis • syndication (atom/rss feeds) • sitemapsFriday, November 2, 12
  4. 4. https://djangoproject.com/Friday, November 2, 12
  5. 5. Community • 3rd-party, open source apps • django-registration • django-social-auth • django-taggit • django-gravatar2 • django-relationshipsFriday, November 2, 12
  6. 6. http://djangopackages.com/Friday, November 2, 12
  7. 7. So, who’s actually using Django?Friday, November 2, 12
  8. 8. Disqus, Instagram, Pintrest, Mozilla, Rdio, Bitbucket, Work for Pie, GiantBomb, The OnionFriday, November 2, 12
  9. 9. Projects & Apps • Projects are a collection of applications • Settings • DB Connections • installed apps • Filesystem paths • Command-line tool: manage.pyFriday, November 2, 12
  10. 10. Projects & Apps $ django-admin.py startproject sampleprojectFriday, November 2, 12
  11. 11. Projects & Apps sampleproject/ !"" manage.py #"" sampleproject !"" __init__.py !"" settings.py !"" urls.py #"" wsgi.pyFriday, November 2, 12
  12. 12. Projects & Apps $ python manage.py startapp blogFriday, November 2, 12
  13. 13. Projects & Apps sampleproject/ !"" blog    !"" __init__.py    !"" models.py    !"" tests.py    #"" views.pyFriday, November 2, 12
  14. 14. Models from django.db import models from django.contrib.auth.models import User class Post(models.Model): author = models.ForeignKey(User) title = models.CharField(max_length=128) slug = models.SlugField(max_length=128, unique=True) content = models.TextField() published_on = models.DateTimeField(auto_now_add=True) sampleproject/blog/models.pyFriday, November 2, 12
  15. 15. syncdb $ python manage.py syncdbFriday, November 2, 12
  16. 16. Views from django.shortcuts import render_to_response from django.template import RequestContext from models import Post def display_post(request, post_slug): post = Post.objects.get(slug=post_slug) template_data = {post: post} template = "blog/post.html" return render_to_response( template, template_data, context_instance=RequestContext(request) ) sampleproject/blog/views.pyFriday, November 2, 12
  17. 17. Views from django.shortcuts import render_to_response from django.template import RequestContext from models import Post def display_post(request, post_slug): post = Post.objects.get(slug=post_slug) template_data = {post: post} template = "blog/post.html" return render_to_response( template, template_data, context_instance=RequestContext(request) ) sampleproject/blog/views.pyFriday, November 2, 12
  18. 18. Views from django.shortcuts import render_to_response from django.template import RequestContext from models import Post def display_post(request, post_slug): post = Post.objects.get(slug=post_slug) template_data = {post: post} template = "blog/post.html" return render_to_response( template, template_data, context_instance=RequestContext(request) ) sampleproject/blog/views.pyFriday, November 2, 12
  19. 19. Views from django.shortcuts import render_to_response from django.template import RequestContext from models import Post def display_post(request, post_slug): post = Post.objects.get(slug=post_slug) template_data = {post: post} template = "blog/post.html" return render_to_response( template, template_data, context_instance=RequestContext(request) ) sampleproject/blog/views.pyFriday, November 2, 12
  20. 20. Views from django.shortcuts import render_to_response from django.template import RequestContext from models import Post def display_post(request, post_slug): post = Post.objects.get(slug=post_slug) template_data = {post: post} template = "blog/post.html" return render_to_response( template, template_data, context_instance=RequestContext(request) ) sampleproject/blog/views.pyFriday, November 2, 12
  21. 21. Views from django.shortcuts import render_to_response from django.template import RequestContext from models import Post def display_post(request, post_slug): post = Post.objects.get(slug=post_slug) template_data = {post: post} template = "blog/post.html" return render_to_response( template, template_data, context_instance=RequestContext(request) ) sampleproject/blog/views.pyFriday, November 2, 12
  22. 22. A base template <!DOCTYPE html> <html> <head> <title> {% block title %}{% endblock %} </title> </head> <body> {% block content %}{% endblock %} </body> </html> sampleproject/sampleproject/templates/base.htmlFriday, November 2, 12
  23. 23. A base template <!DOCTYPE html> <html> <head> <title> {% block title %}{% endblock %} </title> </head> <body> {% block content %}{% endblock %} </body> </html> sampleproject/sampleproject/templates/base.htmlFriday, November 2, 12
  24. 24. A post template {% extends "base.html" %} {% block title %} {{ post.title }} {% endblock %} {% block content %} <h1>{{ post.title }}</h1> {{ post.content|urlize|linebreaks }} <p>Published on: {{ post.published_on|date:"M d, Y" }}</p> {% endblock %} sampleproject/blog/templates/blog/post.htmlFriday, November 2, 12
  25. 25. A post template {% extends "base.html" %} {% block title %} {{ post.title }} {% endblock %} {% block content %} <h1>{{ post.title }}</h1> {{ post.content|urlize|linebreaks }} <p>Published on: {{ post.published_on|date:"M d, Y" }}</p> {% endblock %} sampleproject/blog/templates/blog/post.htmlFriday, November 2, 12
  26. 26. A post template {% extends "base.html" %} {% block title %} {{ post.title }} {% endblock %} {% block content %} <h1>{{ post.title }}</h1> {{ post.content|urlize|linebreaks }} <p>Published on: {{ post.published_on|date:"M d, Y" }}</p> {% endblock %} sampleproject/blog/templates/blog/post.htmlFriday, November 2, 12
  27. 27. A post template {% extends "base.html" %} {% block title %} {{ post.title }} {% endblock %} {% block content %} <h1>{{ post.title }}</h1> {{ post.content|urlize|linebreaks }} <p>Published on: {{ post.published_on|date:"M d, Y" }}</p> {% endblock %} sampleproject/blog/templates/blog/post.htmlFriday, November 2, 12
  28. 28. A post template {% extends "base.html" %} {% block title %} {{ post.title }} {% endblock %} {% block content %} <h1>{{ post.title }}</h1> {{ post.content|urlize|linebreaks }} <p>Published on: {{ post.published_on|date:"M d, Y" }}</p> {% endblock %} sampleproject/blog/templates/blog/post.htmlFriday, November 2, 12
  29. 29. A post template {% extends "base.html" %} {% block title %} {{ post.title }} {% endblock %} {% block content %} <h1>{{ post.title }}</h1> {{ post.content|urlize|linebreaks }} <p>Published on: {{ post.published_on|date:"M d, Y" }}</p> {% endblock %} sampleproject/blog/templates/blog/post.htmlFriday, November 2, 12
  30. 30. A post template {% extends "base.html" %} {% block title %} {{ post.title }} {% endblock %} {% block content %} <h1>{{ post.title }}</h1> {{ post.content|urlize|linebreaks }} <p>Published on: {{ post.published_on|date:"M d, Y" }}</p> {% endblock %} sampleproject/blog/templates/blog/post.htmlFriday, November 2, 12
  31. 31. A post template {% extends "base.html" %} {% block title %} {{ post.title }} {% endblock %} {% block content %} <h1>{{ post.title }}</h1> {{ post.content|urlize|linebreaks }} <p>Published on: {{ post.published_on|date:"M d, Y" }}</p> {% endblock %} sampleproject/blog/templates/blog/post.htmlFriday, November 2, 12
  32. 32. URL Conf’s • Tie it all together! • Route HTTP requests to views • May also capture valuesFriday, November 2, 12
  33. 33. Root URLConf from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns(, url(r^blog/(?P<post_slug>.+)/$, blog.views.display_post, name=display_post), url(r^admin/, include(admin.site.urls)), ) sampleproject/sampleproject/urls.pyFriday, November 2, 12
  34. 34. Root URLConf from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns(, url(r^blog/(?P<post_slug>.+)/$, blog.views.display_post, name=display_post), url(r^admin/, include(admin.site.urls)), ) sampleproject/sampleproject/urls.pyFriday, November 2, 12
  35. 35. Root URLConf from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns(, url(r^blog/(?P<post_slug>.+)/$, blog.views.display_post, name=display_post), url(r^admin/, include(admin.site.urls)), ) sampleproject/sampleproject/urls.pyFriday, November 2, 12
  36. 36. Root URLConf from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns(, url(r^blog/(?P<post_slug>.+)/$, blog.views.display_post, name=display_post), url(r^admin/, include(admin.site.urls)), ) sampleproject/sampleproject/urls.pyFriday, November 2, 12
  37. 37. Root URLConf from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns(, url(r^blog/(?P<post_slug>.+)/$, blog.views.display_post, name=display_post), url(r^admin/, include(admin.site.urls)), ) sampleproject/sampleproject/urls.pyFriday, November 2, 12
  38. 38. Root URLConf from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns(, url(r^blog/(?P<post_slug>.+)/$, blog.views.display_post, name=display_post), url(r^admin/, include(admin.site.urls)), ) sampleproject/sampleproject/urls.pyFriday, November 2, 12
  39. 39. An HTTP RequestFriday, November 2, 12
  40. 40. An HTTP RequestFriday, November 2, 12
  41. 41. An HTTP Request blog/sample-title/ from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns(, url(r^blog/(?P<post_slug>.+)/$, blog.views.display_post, name=display_post), url(r^admin/, include(admin.site.urls)), ) sampleproject/sampleproject/urls.pyFriday, November 2, 12
  42. 42. An HTTP Request blog/sample-title/ from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns(, url(r^blog/(?P<post_slug>.+)/$, blog.views.display_post, name=display_post), url(r^admin/, include(admin.site.urls)), ) sampleproject/sampleproject/urls.pyFriday, November 2, 12
  43. 43. An HTTP Request sample-title def display_post(request, post_slug): post = Post.objects.get(slug=post_slug) template_data = {post: post} template = "blog/post.html" return render_to_response( template, template_data, context_instance=RequestContext(request) ) sampleproject/blog/views.pyFriday, November 2, 12
  44. 44. An HTTP Request {% extends "base.html" %} Sample Title {% block title %} {{ post.title }} {% endblock %} {% block content %} <h1>{{ post.title }}</h1> {{ post.content|urlize|linebreaks }} <p>Published on: {{ post.published_on|date:"M d, Y" }}</p> {% endblock %} sampleproject/blog/templates/blog/post.htmlFriday, November 2, 12
  45. 45. An HTTP Request {% extends "base.html" %} {% block title %} {{ post.title }} {% endblock %} Lorem Ipsum... {% block content %} <h1>{{ post.title }}</h1> {{ post.content|urlize|linebreaks }} <p>Published on: {{ post.published_on|date:"M d, Y" }}</p> {% endblock %} sampleproject/blog/templates/blog/post.htmlFriday, November 2, 12
  46. 46. An HTTP Request {% extends "base.html" %} {% block title %} {{ post.title }} {% endblock %} {% block content %} <h1>{{ post.title }}</h1> Nov 3, 2012 {{ post.content|urlize|linebreaks }} <p>Published on: {{ post.published_on|date:"M d, Y" }}</p> {% endblock %} sampleproject/blog/templates/blog/post.htmlFriday, November 2, 12
  47. 47. An HTTP RequestFriday, November 2, 12
  48. 48. Friday, November 2, 12
  49. 49. A Typical Stack LinuxFriday, November 2, 12
  50. 50. A Typical Stack PostgreSQL LinuxFriday, November 2, 12
  51. 51. A Typical Stack Gunicorn + Django PostgreSQL LinuxFriday, November 2, 12
  52. 52. A Typical Stack nginx Gunicorn + Django PostgreSQL LinuxFriday, November 2, 12
  53. 53. A Typical Stack Varnish Memcached nginx RabbitMQ Gunicorn + Django Redis PostgreSQL LinuxFriday, November 2, 12
  54. 54. Friday, November 2, 12
  55. 55. Enter: Heroku • Deploy & Scale in the Cloud • Provides on-demand App/DB servers • The Heroku Toolbelt • http://www.heroku.com/Friday, November 2, 12
  56. 56. Deploying to Heroku Do a little bit of setup... $ heroku create Creating app-name... done, stack is cedar http://app-name.herokuapp.com/ | git@heroku.com:app-name.git Git remote heroku addedFriday, November 2, 12
  57. 57. Deploying to Heroku $ git push heroku master ... lots of output ...Friday, November 2, 12
  58. 58. Deploying to Heroku $ heroku run python manage.py syncdb ... your typical syncdb output ...Friday, November 2, 12
  59. 59. Deploying to Heroku Develop locally, then when you want to deploy, just run: $ git push heroku masterFriday, November 2, 12
  60. 60. Want to Learn More? • Official Django Docs • https://docs.djangoproject.com • *Djangobook http://www.djangobook.com • Find Apps: http://www.djangopackages.com/ • Coming Soon: http://gettingstartedwithdjango.com/Friday, November 2, 12
  61. 61. Q.E.D.Friday, November 2, 12

×