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

Django: Community and Conventions Primer

Django: Community and Conventions Primer

Given at Kansas City Developers Conference 2014

Garrett Pennington

May 17, 2014
Tweet

More Decks by Garrett Pennington

Other Decks in Programming

Transcript

  1. GOALS 1. Basic Tutorial of Django 2. Have an understanding

    of the common tools used in the Django world 3. Introduction to Django community
  2. ABOUT DJANGO • Python Web Framework • Created at the

    World company in Lawrence, KS by Adrian Holovaty, Simon Wilson, others • Open Source’d in 2005 under BSD License
 https://github.com/django/django

  3. ABOUT DJANGO • Major sites include: • Pintrest • Instragram

    • Mozilla (Add-ons) • Disqus • Rdio
  4. ABOUT DJANGO • Strengths • Developer Friendly • Included suite

    of apps • DRY friendly features • Build a whole lot for very little • Weaknesses • Monolithic (batteries included) • Swapping out pieces reduces their value as a whole. 

  5. CODE OF CONDUCT • Code of Conduct
 https://www.djangoproject.com/conduct/ • Django

    Software Foundation
 https://www.djangoproject.com/foundation/

  6. PIP • Awesome installation and package manager utility. • pip

    (Pip Installs Python)
 pip.readthedocs.org • Installs from PyPI (Python Package Index)
 https://pypi.python.org/pypi
  7. VIRTUALENV • virtualenv allows you to isolate python projects •

    Do this, even if you have one project • http://virtualenv.readthedocs.org/ • Authors: Ian Bicking, Jannis Leidel, Carl Meyer and Brian Rosner pip install virtualenv
  8. VIRTUALENVWRAPPER • Handy extensions for virtualenv • Map projects to

    virtual environments • http://virtualenvwrapper.readthedocs.org • Authors: Doug Hellmann
 
 • Quick wrapper demo pip install virtualenvwrapper
  9. DJANGO PEOPLE Doug Hellmann @doughellmann github.com/dhellmann Projects: virtualenvwrapper
 Python Standard

    Library by Example (http://www.amazon.com/ Python-Standard-Library-Example- Developers/dp/0321767349)

  10. DID YOU READ IT? Notice anything about where Python docs

    live? https://readthedocs.org/ Hosted Sphinx generated documentation. Product of Django Dash. ! ! ahem...http://pymarvel.readthedocs.org
  11. Pip installed. Virtualenv Activated. Now? Okay, now. ! ! !

    INSTALLING DJANGO ! ! pip install django
 pip install django==1.6.5
 pip install django>=1.6,<1.7.0
 pip uninstall django
  12. ! ! pip install -r requirements.txt Django is merely a

    dependency for your WSGI project. You’ll likely have more. Pip can take a requirements file. Add it to source control. You’re dependencies are now versioned and tracked. ! ! ! INSTALLING DJANGO AND FRIENDS
  13. DJANGO-ADMIN.PY • Installing Django puts django-admin.py into your virtualenv bin

    directory. ! • Adds boilerplate, including WSGI and settings module. ! ! • Create or borrow a project template if you don’t like default. (I don’t) django-admin.py startproject myproject ! django-admin.py startproject -- template=<path to template> myproject
  14. PROJECT ORGANIZATION /myproject
 /apps
 /foo_app
 /bar_app
 /settings
 /common.py
 /develop.py
 /hosted.py


    /alpha.py
 /production.py
 /conf
 requirements.txt
 myproject.wsgi
 fabfile.py
 urls.py
  15. PROJECT VS APPLICATION Project:
 Django instance
 Settings Module Application:
 Python

    package (possibly reusable)
 Added to Django settings INSTALLED_APPS ! Let’s make a blog app!
  16. URLS • Uses regular expressions to find a match with

    requested URL • Matched expression passes request to view • URLs can pass parameters • URLs can be named
  17. VIEWS • A view accepts a request, returns an HTTP

    Response • Any response type, code • Convention is views.py • Views should be small • django.shortcuts.render • Can be method or class (CBV)
  18. TEMPLATES • Templates are rendered using available Context • Context

    is a dictionary of values • Lists, Dictionaries, Strings, etc... • Querysets (List-like sets of Model queries) • Vast library of tags and filters-- or custom! • Too much logic in template is Code Smell
  19. MODELS • Python Class • Generally 1:1 Model class to

    DB Table • ORM (Object Relational Mapping) • Database agnostic • Methods for querying, creating data • Business Logic (fat models, skinny views)
  20. `

  21. ADMIN • Easy, free way to being development • Very

    customizable • Drawbacks: • Tied to Django’s ORM • Not suitable for end-users • Essentially a database tool, not a front facing UI • Quick Admin Demo
  22. MODEL METHODS • Models have methods for interacting with an

    instance of data. • First, let’s add a detail view for an instance.
  23. MODEL METHODS • Standard convention is to add __unicode__ method

    to provide handy way to represent an instance of a Model. • Also a good idea to provide a get_absolute_url method to determine where this resource lives.
  24. MANGERS • Managers provide methods for interacting with sets of

    data. • You can write custom managers. ! ! FooModel.objects <-- Model Manager
 FooModel.objects.all() <-- Manager method
 FooModel.objects.featured() <-- Custom manager method !
  25. ! ! pip install south
 <Make model schema changes...>
 MIGRATIONS

    • Django 1.7 provides out of the box schema migrations.
 (But 1.7 isn’t officially out yet...) • Use South for migrations! • South generates Migration files, which you can add to source control.
  26. MIGRATIONS ! ! $ python manage.py schemamigration blog --auto +

    Added field sub_title on blog.Post Created 0002_auto__add_field_post_sub_title.py. You can now apply this migration with: ./manage.py migrate blog
  27. MIGRATIONS ! ! $ python manage.py migrate blog Running migrations

    for blog: - Migrating forwards to 0002_auto__add_field_post_sub_title. > blog:0002_auto__add_field_post_sub_title
  28. FABRIC • http://www.fabfile.org/ • Use Python to write remote (or

    local!) shell commands • Commonly used to deploy applications or perform maintenance tasks across any number of hosts. fab production deploy:2.0.4 fab run_local_server fab alpha kick_apache