Django   a whirlwind tour
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Django a whirlwind tour

on

  • 675 views

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

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

Statistics

Views

Total Views
675
Views on SlideShare
675
Embed Views
0

Actions

Likes
1
Downloads
9
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Django a whirlwind tour Presentation Transcript

  • 1. Django: A Whirlwind Tour Brad Montgomery Email: brad@workforpie.com Twitter: bkmontgomeryFriday, November 2, 12
  • 2. Django: Features • Python • ORM: Object-Relational Mapper • MVC-inspired (MVT) • Clean URLs • Huge Community • Worlds Best DocumentationFriday, November 2, 12
  • 3. Batteries Included aka: contrib apps • admin • auth • comments • gis • syndication (atom/rss feeds) • sitemapsFriday, November 2, 12
  • 4. https://djangoproject.com/Friday, November 2, 12
  • 5. Community • 3rd-party, open source apps • django-registration • django-social-auth • django-taggit • django-gravatar2 • django-relationshipsFriday, November 2, 12
  • 6. http://djangopackages.com/Friday, November 2, 12
  • 7. So, who’s actually using Django?Friday, November 2, 12
  • 8. Disqus, Instagram, Pintrest, Mozilla, Rdio, Bitbucket, Work for Pie, GiantBomb, The OnionFriday, November 2, 12
  • 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. Projects & Apps $ django-admin.py startproject sampleprojectFriday, November 2, 12
  • 11. Projects & Apps sampleproject/ !"" manage.py #"" sampleproject !"" __init__.py !"" settings.py !"" urls.py #"" wsgi.pyFriday, November 2, 12
  • 12. Projects & Apps $ python manage.py startapp blogFriday, November 2, 12
  • 13. Projects & Apps sampleproject/ !"" blog    !"" __init__.py    !"" models.py    !"" tests.py    #"" views.pyFriday, November 2, 12
  • 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. syncdb $ python manage.py syncdbFriday, November 2, 12
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. URL Conf’s • Tie it all together! • Route HTTP requests to views • May also capture valuesFriday, November 2, 12
  • 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. 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. 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. 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. 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. 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. An HTTP RequestFriday, November 2, 12
  • 40. An HTTP RequestFriday, November 2, 12
  • 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. 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. 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. 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. 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. 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. An HTTP RequestFriday, November 2, 12
  • 48. Friday, November 2, 12
  • 49. A Typical Stack LinuxFriday, November 2, 12
  • 50. A Typical Stack PostgreSQL LinuxFriday, November 2, 12
  • 51. A Typical Stack Gunicorn + Django PostgreSQL LinuxFriday, November 2, 12
  • 52. A Typical Stack nginx Gunicorn + Django PostgreSQL LinuxFriday, November 2, 12
  • 53. A Typical Stack Varnish Memcached nginx RabbitMQ Gunicorn + Django Redis PostgreSQL LinuxFriday, November 2, 12
  • 54. Friday, November 2, 12
  • 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. 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. Deploying to Heroku $ git push heroku master ... lots of output ...Friday, November 2, 12
  • 58. Deploying to Heroku $ heroku run python manage.py syncdb ... your typical syncdb output ...Friday, November 2, 12
  • 59. Deploying to Heroku Develop locally, then when you want to deploy, just run: $ git push heroku masterFriday, November 2, 12
  • 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. Q.E.D.Friday, November 2, 12