This document introduces Django, an open-source Python web framework. It describes key Django features like an object-relational mapper, automatic admin interface, template system, caching, and internationalization. It also discusses how Discovery Creative, an in-house ad agency, uses Django for rapid application development and cost savings. Examples are provided of common Django project structures and usage of features like URL design and template tags.
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
Django at the Disco
1. Django at the Disco
An introduction to the Django Web framework and
how it's used by the Discovery Creative team
Rich Leland
Discovery Creative
@richleland
richard_leland@discovery.com
http://creative.discovery.com
2. What is ?
• Python Web framework
• Originally developed for The World Company
• Named after jazz guitarist Django Reinhardt
• Released under BSD license in 2005
3. Who’s using it?
• NASA
• PBS
• Discovery
• National Geographic
• The New York Times
• Broadway
• WNYC
• Google
• LA Times
• LexisNexis
5. Key Features
• Object-relational mapper
• Automatic admin interface
• Elegant, Regex-based URL design
• Template language
• Cache system
• Internationalization
• Community
6. Object-relational mapper
from django.db import models
class Network(models.Model):
"""
Represents a member of our family of Networks.
"""
visible = models.BooleanField(default=True)
name = models.CharField(max_length=100)
slug = models.SlugField(unique=True)
large_logo = models.ImageField(upload_to='ugc/logos/lg/', blank=True)
small_logo = models.ImageField(upload_to='ugc/logos/sm/', blank=True)
link = models.URLField(verify_exists=False, blank=True)
...
# in another file, e.g. views.py:
from myapp.models import Network
Network.objects.all()
Network.objects.get(pk=5)
Network.objects.filter(slug=”some-network”)
10. Elegant, RegEx-based URL design
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^admin/(.*)', admin.site.root),
# included url confs from other apps
(r'^account/', include('registration.urls')),
(r'^bookmarks/', include('bookmarks.urls')),
(r'^messages/', include('mailer.urls')),
# named url patterns pointing to a view
url(r'^downloads/add/(?P<content_type_id>[d]+)/(?P<object_id>[d]+)/$',
'pressweb.media.views.add_download',
name='add-download'),
url(r'^$',
'pressweb.network.views.region_list',
name='select-region-list'),
)
11. URL structure breakdown
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
RegEx pattern include of another urls.py
RegEx pattern
url(r'^networks/(?P<object_id>[d]+)/$',
view='myapp.views.network_detail', views.py
name='add-download'), pattern name
15. Cache system
Template fragment caching
{% load cache %}
{% cache 500 sidebar %}
.. sidebar ..
{% endcache %}
Low-level cache API
from django.core.cache import cache
cache.set('my_key', 'hello, world!', 30)
cache.get('my_key')
16. Internationalization
i18n in templates
{% trans "Bookmarks" %}
{% blocktrans %}This object is called {{ obj.name }}{% endblocktrans %}
i18n in modules
from django.utils.translation import ugettext_lazy as _
class MyThing(models.Model):
name = models.CharField(_('name'), help_text=_('This is the help text'))
Create message files (.po), compile them (.mo)
django-admin.py makemessages -l pt_BR
django-admin.py compilemessages
18. Community
• Core devs actively involved
• Thousands of community-contributed apps
• IRC: #django and #django-dev
• Mailing lists: django-users and django-developers
• Twitter: @djangotracker
19. Discovery Creative
• In-house ad agency for Discovery and family of
networks
• Established 1996
• 100 strong: design, dev, photo, copy, account
services, motion, prepress, production
• Web team established 2006
• 2 Django devs
• 4 Designers/front-end devs
• 2 Project managers
• 1 QA manager
• Located in Silver Spring & London
• Service all networks and business units globally
20. How we’re using Django
• Rapid application development
• Combination of B2B, B2C, in-house apps (directory)
• Working with partners (Siemens, AFI/Silverdocs)
• Saving the company money: 2007: $900k, 2008:
$1.5M