Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Django - A whirlwind tour

Django - A whirlwind tour

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

Brad Montgomery

November 03, 2012
Tweet

More Decks by Brad Montgomery

Other Decks in Programming

Transcript

  1. Django: Features • Python • ORM: Object-Relational Mapper • MVC-inspired

    (MVT) • Clean URLs • Huge Community • Worlds Best Documentation Friday, November 2, 12
  2. Batteries Included aka: contrib apps • admin • auth •

    comments • gis • syndication (atom/rss feeds) • sitemaps Friday, November 2, 12
  3. Community • 3rd-party, open source apps • django-registration • django-social-auth

    • django-taggit • django-gravatar2 • django-relationships Friday, November 2, 12
  4. Projects & Apps • Projects are a collection of applications

    • Settings • DB Connections • installed apps • Filesystem paths • Command-line tool: manage.py Friday, November 2, 12
  5. Projects & Apps sampleproject/ !"" manage.py #"" sampleproject !"" __init__.py

    !"" settings.py !"" urls.py #"" wsgi.py Friday, November 2, 12
  6. Projects & Apps sampleproject/ !"" blog !"" __init__.py !"" models.py

    !"" tests.py #"" views.py Friday, November 2, 12
  7. 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.py Friday, November 2, 12
  8. 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.py Friday, November 2, 12
  9. 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.py Friday, November 2, 12
  10. 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.py Friday, November 2, 12
  11. 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.py Friday, November 2, 12
  12. 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.py Friday, November 2, 12
  13. 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.py Friday, November 2, 12
  14. A base template <!DOCTYPE html> <html> <head> <title> {% block

    title %}{% endblock %} </title> </head> <body> {% block content %}{% endblock %} </body> </html> sampleproject/sampleproject/templates/base.html Friday, November 2, 12
  15. A base template <!DOCTYPE html> <html> <head> <title> {% block

    title %}{% endblock %} </title> </head> <body> {% block content %}{% endblock %} </body> </html> sampleproject/sampleproject/templates/base.html Friday, November 2, 12
  16. 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.html Friday, November 2, 12
  17. 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.html Friday, November 2, 12
  18. 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.html Friday, November 2, 12
  19. 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.html Friday, November 2, 12
  20. 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.html Friday, November 2, 12
  21. 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.html Friday, November 2, 12
  22. 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.html Friday, November 2, 12
  23. 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.html Friday, November 2, 12
  24. URL Conf’s • Tie it all together! • Route HTTP

    requests to views • May also capture values Friday, November 2, 12
  25. 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.py Friday, November 2, 12
  26. 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.py Friday, November 2, 12
  27. 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.py Friday, November 2, 12
  28. 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.py Friday, November 2, 12
  29. 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.py Friday, November 2, 12
  30. 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.py Friday, November 2, 12
  31. An HTTP Request 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.py blog/sample-title/ Friday, November 2, 12
  32. An HTTP Request 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.py blog/sample-title/ Friday, November 2, 12
  33. An HTTP Request 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.py sample-title Friday, November 2, 12
  34. An HTTP Request {% 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.html Sample Title Friday, November 2, 12
  35. An HTTP Request {% 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.html Lorem Ipsum... Friday, November 2, 12
  36. An HTTP Request {% 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.html Nov 3, 2012 Friday, November 2, 12
  37. A Typical Stack Linux PostgreSQL Gunicorn + Django nginx Varnish

    Redis RabbitMQ Memcached Friday, November 2, 12
  38. Enter: Heroku • Deploy & Scale in the Cloud •

    Provides on-demand App/DB servers • The Heroku Toolbelt • http://www.heroku.com/ Friday, November 2, 12
  39. Deploying to Heroku $ heroku create Creating app-name... done, stack

    is cedar http://app-name.herokuapp.com/ | [email protected]:app-name.git Git remote heroku added Do a little bit of setup... Friday, November 2, 12
  40. Deploying to Heroku $ git push heroku master ... lots

    of output ... Friday, November 2, 12
  41. Deploying to Heroku $ heroku run python manage.py syncdb ...

    your typical syncdb output ... Friday, November 2, 12
  42. Deploying to Heroku $ git push heroku master Develop locally,

    then when you want to deploy, just run: Friday, November 2, 12
  43. 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