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

What's New in Django 1.3

What's New in Django 1.3

A talk I gave at DJUGL in May of 2011

Andrew Godwin

May 25, 2011
Tweet

More Decks by Andrew Godwin

Other Decks in Programming

Transcript

  1. Andrew Godwin
    http://www.flickr.com/photos/caroslines/1371200717/
    What's new in Django 1.3?

    View full-size slide

  2. Hi, I'm Andrew.
    Serial Python developer
    Django core committer
    Co-founder of ep.io
    (Python hosting platform)

    View full-size slide

  3. Django: A Very Brief History
    Initial release in 2005
    1.0 released in 2008
    1.3 released March 2011

    View full-size slide

  4. 1.3 was the "bugfix release"
    The plan: No major features, quick release

    View full-size slide

  5. 1.3 was the "bugfix release"
    The plan: No major features, quick release
    The result: 2 major features, 10 months

    View full-size slide

  6. 1.3 was the "bugfix release"
    The plan: No major features, quick release
    The result: 2 major features, 10 months
    We'll get the next one out quicker...

    View full-size slide

  7. New Features
    Class-based views
    Logging
    contrib.staticfiles
    unitttest2
    on_delete
    TemplateResponse

    View full-size slide

  8. Notable Changes
    CSRF on AJAX requests
    Less swearwords
    Translation/i18n improvements
    No more mod_python
    No more XMLField
    render() shortcut

    View full-size slide

  9. Class-Based Views
    Biggest change in 1.3
    Not required for all new views
    Designed to simplify common patterns

    View full-size slide

  10. CBV: Before
    def object_detail(
    request, year, month, day, queryset,
    date_field, month_format='%b',
    day_format='%d', object_id=None,
    slug=None, slug_field='slug',
    template_name=None,
    template_name_field=None,
    template_loader=loader,
    extra_context=None,
    context_processors=None,
    template_object_name='object',
    mimetype=None, allow_future=False
    ):

    View full-size slide

  11. CBV: After
    class AccountDetail(DetailView):
    model = Account
    context_object_name = "account"
    slug_field = "snail"
    def get_context_data(self, **kwargs):
    context = super(AccountDetail, self)\
    .get_context_data(**kwargs)
    context['books'] = Book.objects.all()
    return context

    View full-size slide

  12. CBV: Simple views too!
    class AccountDetail(TemplateView):
    template_name = "mytempl.html"
    def get_context_data(self, **kwargs):
    return {
    "books": Book.objects.all(),
    "is_evil": True,
    }

    View full-size slide

  13. Other uses
    Refactor common parts into superclasses
    Mixins for adding-on functionality
    Mutate the input or output

    View full-size slide

  14. Logging
    Now actually sensible, not just emails
    Uses standard Python logging library
    Configured using the LOGGING setting

    View full-size slide

  15. LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'handlers': {
    'console':{
    'level': 'DEBUG',
    'class': 'logging.StreamHandler',
    },
    'mail_admins': {
    'level': 'ERROR',
    'class': 'django.utils.log.AdminEmailHandler',
    }
    },
    'loggers': {
    'django.request': {
    'handlers': ['mail_admins'],
    'level': 'ERROR',
    },
    'myproject.custom': {
    'handlers': ['console', 'mail_admins'],
    'level': 'INFO',
    }
    }
    }

    View full-size slide

  16. staticfiles
    Lets apps ship with their static files
    View to serve them in development
    ./manage.py collectstatic for production

    View full-size slide

  17. unittest2
    Better than unittest (obviously)
    Many new assertion methods
    Test skipping
    Expected failures
    Much more...

    View full-size slide

  18. unittest2
    Bundled with Django
    Just change:
    import unittest
    from django.utils import unittest
    to

    View full-size slide

  19. on_delete
    Deleting rows just got better!
    Cascade delete (as before)
    Set to NULL
    Raise ProtectedError
    Set to arbitary value

    View full-size slide

  20. TemplateResponse
    A HTTPResponse class you can change
    Template only run at end of middleware
    Change context, or even template

    View full-size slide

  21. TemplateResponse Example
    from django.template.response \
    import TemplateResponse
    def blog_index(request):
    return TemplateResponse(
    request,
    'entry_list.html',
    {'entries': Entry.objects.all()},
    )
    ...
    response.context_data['foo'] = 'bar'

    View full-size slide

  22. CSRF on AJAX requests
    CSRF is a hard, hard problem
    Flash plus 307 redirect = fail
    You'll have to start supplying
    CSRF tokens to your JavaScript

    View full-size slide

  23. Less Swearwords
    Guaranteed, or your money back.
    Also gone: "asshat", "asshead".

    View full-size slide

  24. Translation / i18n
    Marking for ambiguous meanings
    Overrideable translations for apps
    Deprecation of project-wide translations

    View full-size slide

  25. No more mod_python
    It's dead, Jim.
    If you're still using it, move to mod_wsgi
    or gunicorn now, please.

    View full-size slide

  26. No more XMLField
    We're not really sure why it was there
    in the first place.

    View full-size slide

  27. render() shortcut
    Like render_to_response, but :
    Less characters to type
    Uses RequestContext
    Handy for those not on CBV

    View full-size slide

  28. How do you upgrade?
    Should be mostly seamless.
    You'll get warnings for deprecated settings
    (e.g. DATABASE_NAME)

    View full-size slide

  29. What's going to be in 1.4?
    We're not quite sure yet.
    Possible:
    App refactor
    HTML5 doctype
    ORM changes

    View full-size slide

  30. Future features
    GSOC should give us:
    Template engine refactor
    New form rendering
    Schema alteration low-level API
    Composite fields

    View full-size slide

  31. Thank you.
    Andrew Godwin
    @andrewgodwin
    [email protected]

    View full-size slide