Intro To Django

Intro To Django

Slides to a talk given by Sean O'Connor at the NYLUG.

7c4892dabc0e774cd66405b7137f72dc?s=128

Sean O'Connor

November 14, 2013
Tweet

Transcript

  1. Sean O’Connor @theSeanOC

  2. Who is this guy?

  3. None
  4. D J A N G O N Y C

  5. None
  6. None
  7. None
  8. None
  9. None
  10. None
  11. Web Application Framework

  12. gph.is/VwXYWP

  13. None
  14. flickr.com/photos/lungstruck/9282575739

  15. flickr.com/photos/gavinbell/8148213376

  16. flickr.com/photos/lostvegas/2214183472

  17. flickr.com/photos/skinnylawyer/6963788981

  18. flickr.com/photos/_mia/9444997616

  19. flickr.com/photos/wheatfields/118700012

  20. j.mp/1aAUd5D

  21. flickr.com/photos/doug88888/6097007477

  22. flickr.com/photos/armchairbuilder/6198714006

  23. flickr.com/photos/pauldineen/4049601480

  24. flickr.com/photos/9731367@N02/6988181354

  25. flickr.com/photos/96584021@N05/8996280560

  26. flickr.com/photos/cubicgarden/3280483306

  27. flickr.com/photos/doug88888/4718612162

  28. flickr.com/photos/nocklebeast/2334963027

  29. flickr.com/photos/furlined/5721248693

  30. flickr.com/photos/sparr0/4391464964

  31. flickr.com/photos/horiavarlan/4268896468

  32. flickr.com/photos/dia-a-dia/7046151669

  33. flickr.com/photos/36593372@N04/8172275651

  34. “Easy things should be easy, and hard things should be

    possible.” Larry Wall
  35. “It’s just Python”

  36. ORM

  37. from django.db import models ! class Person(models.Model): first_name = models.CharField(max_length=30)

    last_name = models.CharField(max_length=30)
  38. from myapp.models import Person ! chauncey = Person.objects.create(first_name="Chauncey", last_name="McPufferson") print

    chauncey.first_name chauncey.last_name = "McPufferson" chauncey.save() chauncey.delete()
  39. from myapp.models import Person ! person = Person.objects.get(id=1) bobs =

    Person.objects.filter(first_name="Bob") obs = Person.objects.raw( "SELECT * FROM myapp_person WHERE first_name LIKE '%ob%'")
  40. Routes

  41. from django.conf.urls import patterns, include, url ! from polls import

    views ! urlpatterns = patterns('', # ex: /polls/ url(r'^$', views.index, name=‘index'), ! ! ! ! ! ! ! ! ) # ex: /polls/5/ url(r'^(?P<poll_id>\d+)/$', views.detail, name=‘detail'), # ex: /polls/5/results/ url(r'^(?P<poll_id>\d+)/results/$', views.results, name=‘results'), # ex: /polls/special/* url(r'^special/', include('polls.special.urls')),
  42. from django.core.urlresolvers import reverse ! reverse('detail', kwargs={'poll_id': 5})

  43. Views

  44. from django.shortcuts import render from polls.models import Poll ! !

    ! def index(request): ! ! latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5] ! ! context = {'latest_poll_list': latest_poll_list} ! ! return render(request, 'polls/index.html', context)
  45. from django.shortcuts import render, get_object_or_404 from polls.models import Poll !

    ! ! ! def detail(request, poll_id): ! ! poll = get_object_or_404(Poll, pk=poll_id) ! ! return render(request, 'polls/detail.html', {'poll': poll})
  46. from django.views.generic import ListView from polls.models import Poll ! !

    ! class PollList(ListView): model = Poll
  47. Templates

  48. <h1>{{ poll.question }}</h1> ! <ul> {% for choice in poll.choice_set.all

    %} ! <li>{{ choice.choice_text }}</li> ! {% endfor %} </ul>
  49. Forms

  50. from django import forms ! ! class ContactForm(forms.Form): ! subject

    = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() cc_myself = forms.BooleanField(required=False)
  51. <form action="/contact/" method=“post"> ! {{ form.as_p }} ! <input type="submit"

    value="Submit" /> ! </form>
  52. <form action="/contact/" method=“post"> ! <p><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject"

    maxlength="100" /></p> ! <p><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></p> ! <p><label for="id_sender">Sender:</label> <input type="email" name="sender" id="id_sender" /></p> ! <p><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p> ! <input type="submit" value="Submit" /> ! </form>
  53. from django.shortcuts import render from django.http import HttpResponseRedirect ! def

    contact(request): if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): # Process the data in form.cleaned_data # ... return HttpResponseRedirect('/thanks/') else: form = ContactForm() ! return render(request, 'contact.html', { 'form': form, })
  54. Admin

  55. None
  56. Security

  57. Testing

  58. Internationalization

  59. Authenitcation

  60. Caching

  61. Logging

  62. Email Sending

  63. RSS

  64. Messaging

  65. Site Maps

  66. Static Files

  67. flickr.com/photos/velkr0/3472576304

  68. djangoproject.com

  69. None
  70. #django

  71. django-users

  72. D J A N G O N Y C djangonyc.org

  73. us.pycon.org

  74. gettingstartedwithdjango.com

  75. django.2scoops.org

  76. LearnPythonTheHardWay.org

  77. djangopackages.com

  78. Shameless Plug

  79. We’re hiring!

  80. None
  81. Questions?