SlideShare a Scribd company logo
1 of 78
Download to read offline
Web Design and Developer Conference 2008




Jeff Croft
October 16th, 2008
The Web Design Conference, Orlando


Introduction to Django
Web Design and Developer Conference 2008




Introduction
Web Design and Developer Conference 2008




I’m Jeff Croft.
Web Design and Developer Conference 2008




  What do I do?
• Design and develop web and mobile sites and applications for
  Blue Flavor and its clients.

• Use and evangelize Django, an open source, Python-based web
  application framework that originated at The World Company,
  my former employer.

• Write about web design, Django, Mac nerdery, and whatever
  else strikes my fancy at jeffcroft.com.
Web Design and Developer Conference 2008




What is Django?
Web Design and Developer Conference 2008




 From the horse’s mouth:
“ Django is a high-level Python web framework that
  encourages rapid development and clean, pragmatic
  design.”

 djangoproject.com
Web Design and Developer Conference 2008




“...high level Python web framework...”
• A web framework attempts to abstract routine development
  tasks by providing shortcuts.

• For example: dealing with the details of HTTP, connecting to a
  database, managing users and groups, generating feeds, etc.

• However, a good web framework shouldn’t get in your way if
  you want or need to work at this lower level. You should always
  be able to “step down a level.”

• Django is built on Python, an established high-level language
  used heavily at Google, Yahoo, CERN, and NASA.

• Django makes a point of not changing anything about the way
  Python works. If you learn Django, you’re learning Python—and
  vice-versa.
Web Design and Developer Conference 2008




“...encourages rapid development...”
• Saving you time is one of Django’s primary goals.

• Django was created in the fast-paced news world, and was
  designed around a real-world need to develop sites in a matter
  of hours, not days or weeks.
Web Design and Developer Conference 2008




“...clean, pragmatic design.”
• Django is opinionated software. It firmly believes there is a “right”
  way to do many things.

• It tries to make it as easy as possible to do things the “right”
  way. Django helps you subscribe to best practices by making
  them simple.
Web Design and Developer Conference 2008




A bit of Django history
• Created in 2003 at The World Company in Lawrence, KS, by
  Adrian Holovaty and Simon Willison. Originally, it was call “The
  CMS”, and was a project to build a CMS for newspapers.

• As time passed, they abstracted the underlying “stuff to help
  you build web apps” from the higher level “stuff that makes
  running an online newspaper easier.” The two became Django
  (open source) and Ellington (commerical), respectively.

• Django was released as open source software in the summer of
  2005. Since then, it has really taken off, and is in use at many
  major corporations, include Google.

• Predictably, Django became extremely popular in the journalism
  world. In 2007, there were 14 Django-powered sites among the
  finalists in 11 categories for the Digital Edge online journalism
  awards. Most of these were running Ellington.
Web Design and Developer Conference 2008




Continued...
• The core Django developers had several big changes they
  wanted to make that would introduce backwards-
  incompatibilities for developers who’d built sites on Django.
  They decided to make these changes before giving Django the
  1.0 version number. As such, the time between Django’s
  original release and it’s 1.0 release was...well, ridiculous.

• Django 1.0 was released on September 2nd, 2008!

• The good news for you: there should be no significant
  backwards incompatible changes from here on out. The code
  you will learn today will be valid Django code for a very long
  time.
Web Design and Developer Conference 2008




Projects and apps
Web Design and Developer Conference 2008




  Projects in Django
• A project is a configuration for an instance of Django.

• It contains things like database settings, filesystem paths, and
  so forth. It may also contain settings for applications (more on
  applications soon).

• It also contains a list of the applications your project will be
  using.

• Usually, a project is associated with a single website—each
  website has a project that controls it. There are cases where a
  single project can drive multiple websites, but that’s more
  advanced and not for you to worry about right now.
Web Design and Developer Conference 2008




  Apps in Django
• An “app” in Django is a collection of code that makes up some
  sort of functionality for your website.

• A typical project will use several apps.

• Apps can be reused between projects (this is one of Django’s
  coolest features — more on it later).

• Deciding what is an “app” can be tricky. For example, should
  you make an app that handles your blog and comments, or
  should you make two apps (one for blog, one for comments)?

• Generally, it’s best to make two apps. But don’t worry about this
  too much right now. You can always refactor your code into
  multiple apps later, when you start to see how it might be
  beneficial.
Web Design and Developer Conference 2008




  Apps are “pluggable”
• One of Django’s greatest strengths is that a single app can be
  reused in many projects.

• This has fostered a great community around building reusable
  apps that are made available for you to just “plug in” to your
  project.
Web Design and Developer Conference 2008




  Let’s say you have three sites:
• First, you have a personal site. It includes a blog with tags and
  comments, a photo gallery, some “static” pages, and contact
  form.

• You also have a Digg-style social news site. It contains stories
  with tags and comments, the ability for users to vote on stories,
  some “static” pages, and a contact form.

• You also have a movie review site. It contains a database of
  movies with tags, your reviews with tags and comments, the
  ability for users to vote on movies, some “static” pages, and a
  contact form.
Web Design and Developer Conference 2008




  There’s some overlap here.
• All three sites have comments, “static” pages, and a contact
  form.

• Two sites have blogs and photo galleries. Two sites also allow
  users to vote on items.

• Ideally, you build generic apps for these functions, so that they
  can work across all three sites. Clearly, this is better for
  maintainability.
Web Design and Developer Conference 2008




  These already exist.
• Django comes with a comments app. It allows users to
  comment on any sort of item in your database (blog entry, story,
  movie review, etc.)

• Django also comes with an app for handling “static” pages
  (simple pages with content stored in the database).

• django-tagging (http://code.google.com/p/django-tagging/)
  allows you to easily add tag functionality to any sort of item in
  your database.

• django-voting (http://code.google.com/p/django-voting/) allows
  users to vote on any sort of item in your database.

• django-contact-form (http://code.google.com/p/django-
  contact-form/) provides basic contact form functionality.

• There are several available blog and photo gallery applications.
Web Design and Developer Conference 2008




    django.contrib
• Django is distributed with several of these reusable apps.
  They’re all optional, but available for use in your project if you
  want them. Some of them are:

•   django.contrib.admin — Provides an automatic admin interface
•   django.contrib.auth — Provides users and groups
•   django.contrib.comments — Provides commenting functionality
•   django.contrib.flatpages — Handles simple, “static” pages
•   django.contrib.syndication — Makes creating feeds easy
Web Design and Developer Conference 2008




    More examples
• All of the following are available on Google Code (as well as a
  ton of others — these are just a few I thought were useful):

•   django-pagination
•   django-mailfriend
•   django-messages
•   django-forum
•   django-profiles
•   django-friends
•   django-registration
•   django-authopenid
•   django-basic-apps
Web Design and Developer Conference 2008




  Installing an app in a project
• In order to use your app in a project, you need to place a
  reference to it in the INSTALLED_APPS list in your settings.py
  file.

 INSTALLED_APPS = (
     quot;django.contrib.authquot;,
     quot;django.contrib.contenttypesquot;,
     quot;django.contrib.sessionsquot;,
     quot;django.contrib.sitesquot;,
     quot;registrationquot;,
     quot;paginationquot;,
 )
Web Design and Developer Conference 2008




Models
Web Design and Developer Conference 2008




  What are models?
• A model is a description of a particular type of data in your
  application.

• Your models define what types of data will be available in your
  application and what fields and attributes those objects have.

• In Django, model definitions also contain metadata about your
  models.

• In Django, models map one-to-one to database tables.
Web Design and Developer Conference 2008




Typically, you’d do this:

CREATE TABLE quot;people_personquot; (
    quot;idquot; integer NOT NULL PRIMARY KEY,
    quot;first_namequot; varchar(300) NOT NULL,
    quot;last_namequot; varchar(300) NOT NULL,
    quot;bioquot; text NOT NULL,
);
Web Design and Developer Conference 2008




  But not in Django.
• SQL is not consistent from database to database. Django
  projects should easily port from one database environment to
  another without having to change your code. Django models
  work with any database Django supports.

• If you store your models in the database, it becomes difficult to
  keep them under version control.

• Your application will need to work with the model regardless of
  how you created it. If you created it in SQL, you’d need to
  recreate it in Python so your application can work with it. Thus,
  Django just has you create models in Python from the start, and
  handles the SQL for you.

• Writing Python is fun! Well, at least it’s a lot more fun than
  writing SQL. If you ask me, anyway. :)
Web Design and Developer Conference 2008




The same model, in Django:
from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=300)
    last_name   = models.CharField(max_length=300)
    bio         = models.TextField()

   def __unicode__(self):
       return (%s %s) % (self.first_name, self.last_name)

    class Meta:
        ordering = [quot;last_namequot;]
        verbose_name = quot;Personquot;
        verbose_name_plural = quot;Peoplequot;
Web Design and Developer Conference 2008




The same model, in Django:
from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=300)
    last_name   = models.CharField(max_length=300)
    bio         = models.TextField()

   def __unicode__(self):
       return (%s %s) % (self.first_name, self.last_name)

    class Meta:
        ordering = [quot;last_namequot;]
        verbose_name = quot;Personquot;
        verbose_name_plural = quot;Peoplequot;
Web Design and Developer Conference 2008




Models can contain relationships
from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=300)
    last_name   = models.CharField(max_length=300)
    bio         = models.TextField()
    ...

class Article(models.Model):
    title      = models.CharField(max_length=300)
    date       = models.DateTimeField()
    author     = models.ForeignKey(Person,related_name= quot;articlesquot;)
Web Design and Developer Conference 2008




  Syncing your database
• Once you’ve created models, Django will run the necessary
  SQL to install them into the database for you. You just need to
  run the manage.py command “syncdb”

  $ ./manage.py syncdb
  Creating table my_first_model
  Creating table my_second_model
  ...

• This will install all models for any app in your project’s
  INSTALLED_APPS setting.
Web Design and Developer Conference 2008




  Django’s database API
• Once you’ve installed your models, Django provides a high-
  level API for working with them.

• The API provides functions for creating, retrieving, changing,
  and deleting objects in the database — and a lot more.
Web Design and Developer Conference 2008




  Creating an object
• Assuming the “Person” model I showed earlier, you can create
  a new person in the database as follows:

  p = Person(
      first_name = quot;Jeffquot;,
      last_name = quot;Croftquot;,
      bio = quot;Jeff Croft is awesome.quot;
  )
  p.save()

• Note the explicit .save() method each model gets. This is an
  important bit of Django philosophy: Django never hits the
  database until you explicitly tell it to. Thus, the .save() call is
  required.
Web Design and Developer Conference 2008




  Retrieving objects
• Each model gets a member called “objects,” which is used for
  data retrieval. A few examples:

 Person.objects.all()
 Person.objects.get(id=10)
 Person.objects.filter(first_name=quot;jeffquot;)
 Person.objects.exclude(first_name__contains=quot;jequot;)
 Person.objects.all().order_by(quot;first_namequot;)
 Article.objects.filter(author__first_name=quot;jeffquot;)

• You can chain these things. You can do, for example:
  Person.objects.filter().exclude().order_by()

• The Django database API is very powerful, and I’ve personally
  never come across a situation where it couldn’t do what I
  needed it to do. But, if there is a case when you need to write
  SQL directly, Django will let you do that.
Web Design and Developer Conference 2008




Using relationships

p = Person.objects.get(first_name=quot;Jeffquot;, last_name=quot;Croftquot;)
p.articles.all()
p.articles.filter(title__contains=quot;djangoquot;)

a = Article.objects.get(title=quot;Django is greatquot;)
a.author
a.author.first_name
Web Design and Developer Conference 2008




Editing an object

p = Person.objects.get(first_name=quot;Jeffquot;, last_name=quot;Croftquot;)
p.bio = quot;No, really. Jeff Croft is totally awesome.quot;
p.save()
Web Design and Developer Conference 2008




Deleting an object

p = Person.objects.get(first_name=quot;Jeffquot;, last_name=quot;Croftquot;)
p.delete()
Web Design and Developer Conference 2008




  Lazy QuerySets
• When you pull items from the database, Django calls the result
  a “QuerySet.” It’s more or less a Python list with some added
  functionality. QuerySets are lazy. That is to say, they only hit the
  database when they're evaluated. For example:
  people = Person.objects.filter(first_name=quot;jeffquot;)
  people = people.exclude(last_name__contains=quot;croftquot;)
  people = people.order_by(quot;last_namequot;)
  print people   # Database hit

• This code executes exactly one database query.
Web Design and Developer Conference 2008




Django admin
Web Design and Developer Conference 2008




  What is the Django admin?
• The Django admin is one of the afore mentioned optional
  “contrib” apps that are distributed with Django itself.

• It provides a powerful, customizable, production-ready interface
  to creating, updating, and deleting instances of your models.

• It includes data entry validation, user and group permissions,
  and a log of user activity in the Admin.

• It is designed for regular users. It’s not like phpMyAdmin or
  other database administration tools. It was built at a newspaper
  so that editors and producers could enter and update content.

• The Django admin works off your models alone. Even if your
  app is not done, once you have the models in place, you can
  start working with the Django admin.
Web Design and Developer Conference 2008
Web Design and Developer Conference 2008
Web Design and Developer Conference 2008
Web Design and Developer Conference 2008




  Why? And what if I don’t want it?
• Writing admin interfaces sucks. It’s boring, repetitive, and just
  usually not that much fun.

• The admin interface is entirely optional. It’s just a Django app
  you plug into your project like any other. If you don’t want it,
  you just don’t plug it in.

• If you want or need to write your own administrative interface
  for your project, you can certainly do so.
Web Design and Developer Conference 2008




Views
Web Design and Developer Conference 2008




  What are views?
• A view is (generally) a “type” of web page within your
  application.

• A view (generally) performs a specific function and renders a
  specific template.

• A view is simply a Python function that takes an HTTP request
  and returns an HTTP response.

• View functions can live anywhere in your Python path, but are
  typically put into the views.py file for your application.
Web Design and Developer Conference 2008




  A view is not “how it looks.”
• In most cases, the view is responsible for pulling together all
  the information that’s available at a given URL. It may look up
  information in the database, perform some kind of processing
  on data, process input from a form, send an e-mail, etc.

• Since a view is just a Python function, it can really do anything
  you want it to. The only rules are that it must take an HTTP
  request and return an HTTP response.

• The view shouldn’t be outputting (X)HTML directly. Rather, it will
  render through a template, which is where the (X)HTML is
  structured. We’ll deal with templates later.
Web Design and Developer Conference 2008




A simple first view:
from django.http import HttpResponse

def homepage(request):
    return HttpResponse(quot;Welcome to the homepage!quot;)
Web Design and Developer Conference 2008




A less simple view:
from django.shortcuts import render_to_response
from myproject.myapp import Person

def people_list(request):
    people = Person.objects.all()
    return render_to_response(
        quot;people/list.htmlquot;,
        {quot;people_listquot;: people},
    )
Web Design and Developer Conference 2008




Another example:
from django.shortcuts import render_to_response, get_object_or_404
from myproject.myapp import Person

def person_detail(request, lastname):
    person = get_object_or_404(Person, last_name=lastname)
    return render_to_response{
        quot;people/detail.htmlquot;,
        {quot;personquot;: person},
    }
Web Design and Developer Conference 2008




Generic views
Web Design and Developer Conference 2008




  What are generic views?
• Generic views are a set of regular Django views that are
  distributed with Django and cover a large variety of standard
  use cases.

• They save you time, by making you not have to write views in
  “typical” situations. You can just use the included generic
  views, instead.

• In many cases, generic views are all (or most of what) you need.
  Simple sites often use exclusively generic views. At the
  newspapers I worked for, generic views covered around 85% of
  the pages on the site.
Web Design and Developer Conference 2008




    Django provides:
•   django.views.generic.simple
•   django.views.generic.date_based
•   django.views.generic.list_detail
•   django.views.generic.create_update
Web Design and Developer Conference 2008




URLconfs
Web Design and Developer Conference 2008




  What are URLconfs?
• In Django, you explicitly define which URLs map to which
  functions. The URLs are not implicit from the filesystem, from
  the view name, or from anything else. You must consciously
  think about what your URLs should look like and how they
  should work.

• A “URLconf”, as Django people call them, is a mapping of
  URLs to view functions. For example, if someone visiting the
  URL http://yoursite.com, the URL conf points them to the view
  function you’ve written for your homepage. If they go to http://
  yoursite.com/blog, the URL conf points them to the view
  function you’ve written for your blog index. And so forth.

• Django uses regular expressions to match URLs.

• URLconfs can live anywhere in your Python path, but typically
  each application will have a urls.py file outlining the URLs it
  provides, and each project will also have a urls.py file.
Web Design and Developer Conference 2008




    Why? Because these are ugly.
•   page.php
•   script.cgi?pageid=144
•   StoryPage.aspx
•   0,2097,1-1-30-72-407-4752,00.html
•   /stories/147/

• URLs are part of your application design. You should think
  about them as a key part of the interface to your site and
  design them with the same level of attention you’d design any
  other interface element.
Web Design and Developer Conference 2008




  Django URLs look more like this:
• /stories/
• /stories/2008/apr/20/michelles-birthday/
• /categories/birthdays/

• Really, Django URLs can look any way you want them to—even
  the ugly ways in the previous slide. But, this sort of scheme is
  typical.
Web Design and Developer Conference 2008




Templates
Web Design and Developer Conference 2008




  What are templates?
• If a view describes what data is on a page, then a template
  defines how that data is presented as HTML (usually).

• While Django’s template language is most commonly used to
  generate (X)HTML, it can actually be used to create any text-
  based file format (e-mail, RSS, CSV, XML, YAML, etc.).

• Django’s template language was designed to be usable by
  designers and HTML authors. It should not require a
  programmer to operate it.
Web Design and Developer Conference 2008




An example template
<!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;>
  <html lang=quot;enquot;>
  <head><title>People</title></head>
  <body>
    <h1>People ({{ people_list|length }} total)</h1>
    <ul>
      {% for p in person_list %}
         <li>
           <a href=quot;{{ p.last_name }}/quot;>
              {{ p.first_name }} {{ p.last_name }}
           </a>
         </li>
      {% endfor %}
    </ul>
  </body>
</html>
Web Design and Developer Conference 2008




  Three main template components
• Variables: Remember how our view provided a “context” to the
  template? That context is the set of variables our template has
  access to. Variables are used like this: {{ variable_name }}

• Tags: Tags are more complex than variables. Some create text
  in the template, some perform loops or logic, some add data to
  the current template context, and some do other stuff. A tag
  can do virtually anything. Tags may take arguments. Tags are
  used like this: {% tag_name %}

• Filters: Filters can be thought of like a pipe, in UNIX. You pass
  something into them, and they return something different. They
  can be strung together, too. Many filters reformat a variable. For
  example, the “date” filter defines how a date variable will be
  outputted. Filters are used like this {{ variable|filter_name }}
Web Design and Developer Conference 2008




Variables
<!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;>
  <html lang=quot;enquot;>
  <head><title>People</title></head>
  <body>
    <h1>{{ person.first_name }} {{ person.last_name }}</h1>
    <p>{{ person.bio }}</p>
  </body>
</html>
Web Design and Developer Conference 2008




Variables
<!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;>
  <html lang=quot;enquot;>
  <head><title>People</title></head>
  <body>
    <h1>{{ person.first_name }} {{ person.last_name }}</h1>
    <p>{{ person.bio }}</p>
  </body>
</html>
Web Design and Developer Conference 2008




Filters
<!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;>
  <html lang=quot;enquot;>
  <head><title>People</title></head>
  <body>
    <h1>People ({{ people_list|length }} total)</h1>
    <ul>
      {% for p in person_list %}
         <li>
           <a href=quot;{{ p.last_name }}/quot;>
              {{ p.first_name|capfirst }}{{ p.last_name|striptags }}
           </a>
         </li>
      {% endfor %}
    </ul>
  </body>
</html>
Web Design and Developer Conference 2008




Filters
<!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;>
  <html lang=quot;enquot;>
  <head><title>People</title></head>
  <body>
    <h1>People ({{ people_list|length }} total)</h1>
    <ul>
      {% for p in person_list %}
         <li>
           <a href=quot;{{ p.last_name }}/quot;>
              {{ p.first_name|capfirst }}{{ p.last_name|striptags }}
           </a>
         </li>
      {% endfor %}
    </ul>
  </body>
</html>
Web Design and Developer Conference 2008




Tags
<!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;>
  <html lang=quot;enquot;>
  <head><title>People</title></head>
  <body>
    <h1>People ({{ people_list|length }} total)</h1>
    <ul>
      {% for p in person_list %}
         <li>
           <a href=quot;{{ p.last_name }}/quot;>
              {{ p.first_name }}{{ p.last_name }}
           </a>
         </li>
      {% endfor %}
    </ul>
  </body>
</html>
Web Design and Developer Conference 2008




Tags
<!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;>
  <html lang=quot;enquot;>
  <head><title>People</title></head>
  <body>
    <h1>People ({{ people_list|length }} total)</h1>
    <ul>
      {% for p in person_list %}
         <li>
           <a href=quot;{{ p.last_name }}/quot;>
             {{ p.first_name }}{{ p.last_name }}
           </a>
         </li>
      {% endfor %}
    </ul>
  </body>
</html>
Web Design and Developer Conference 2008




  Template inheritance
• Django’s template language includes an inheritance mechanism
  that is both extremely powerful and sort of hard to get your
  head around at first.

• You might think of it as an updated (if backwards) take on
  “includes,” which you may have used in the past (SSI, PHP
  includes, etc.)

• The core problem inheritance is trying to solve is one of
  repeated code. Ideally, you Don’t Repeat Yourself (DRY), and
  any time you need to make changes to template code, you only
  have to do it in one place.
Web Design and Developer Conference 2008




  “Blocks”
• Django’s template inheritance centers around the idea of
  “blocks”. A template can define “blocks”—essentially chunks
  another template can override.

• A typical setup is to have a “base” template which provides a
  bunch of empty blocks, and then “child” templates which fill
  them. In Django parlance, child templates “extend” other
  templates.

• Extension doesn’t have to be only two level: you could have a
  “base” template for your site, plus a template for a section of
  your site which extends the base, plus a template that extends
  the section template. And so on.
Web Design and Developer Conference 2008




A “base” template base.html
<!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;>
  <html lang=quot;enquot;>
  <head><title>{% block title %}{% endblock %}</title></head>
  <body>
    <div id=”content”>
      {% block content %}
         <h1>Welcome to our site!</h1>
      {% endblock %}
    </div>
    <div id=”footer”>
      {% block footer %}
         <p>Copyright 2008 Jeff Croft</p>
      {% endblock %}
    </div>
  </body>
</html>
Web Design and Developer Conference 2008




“Blocks”
<!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;>
  <html lang=quot;enquot;>
  <head><title>{% block title %}{% endblock %}</title></head>
  <body>
    <div id=”content”>
      {% block content %}
         <h1>Welcome to our site!</h1>
      {% endblock %}
    </div>
    <div id=”footer”>
      {% block footer %}
         <p>Copyright 2008 Jeff Croft</p>
      {% endblock %}
    </div>
  </body>
</html>
Web Design and Developer Conference 2008




A child template person.html
{% extends “base.html” %}

{% block title %}
  {{ person.first_name }}{{ person.last_name }}
{% endblock %}

{% block content %}
  <h1>About {{ person.first_name }}{{ person.last_name }}</h1>
{% endblock %}
Web Design and Developer Conference 2008




A child template person.html
{% extends “base.html” %}

{% block title %}
  {{ person.first_name }}{{ person.last_name }}
{% endblock %}

{% block content %}
  <h1>About {{ person.first_name }}{{ person.last_name }}</h1>
{% endblock %}
Web Design and Developer Conference 2008




  The beauty of inheritance
• Besides keeping everything DRY, one of the biggest reasons to
  make good use of inheritance is for redesigns: often, replacing
  the base.html template is all you have to do to get a totally new
  layout, look, and feel.
Web Design and Developer Conference 2008




  A note on tags and filters
• Django is distributed with a vast library of template tags and
  filters.

• As you get more advanced, though, you may wish for a
  template tag that doesn’t exist.

• Template tags and filters are just Python functions, so there’s
  nothing to stop you from writing your own. In fact, most Django
  apps provide some template tags (along with their models,
  views, and urls).

• To load a non-built-in set of tags (such as those an application
  might provide), use the “load” tag: {% load blog_tags %}
Web Design and Developer Conference 2008




What else is in Django?
Web Design and Developer Conference 2008




  Some of the things we didn’t cover:
• Forms: Handles HTML form display, data processing
  (validation) and redisplay.

• Caching: Flexible caching at any level, using memcached,
  database, local memory, or filesystem caching.

• Internationalization: Full support for internationalization of
  text.

• Sessions: Full support for anonymous, cookie-based sessions.

• Testing: Full testing suite included.

• Middleware: a framework of hooks into Django’s request/
  response processing. Allow for global altering of Django’s input
  and/or output.
Web Design and Developer Conference 2008




Okay, go write some code!
Web Design and Developer Conference 2008




(or ask me questions.)
Web Design and Developer Conference 2008




Thanks, yo.

More Related Content

What's hot

Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to DjangoKnoldus Inc.
 
Django Introduction & Tutorial
Django Introduction & TutorialDjango Introduction & Tutorial
Django Introduction & Tutorial之宇 趙
 
Introduction to Web Frameworks
Introduction to Web FrameworksIntroduction to Web Frameworks
Introduction to Web FrameworksSarika Jadhav
 
Best Practices for Front-End Django Developers
Best Practices for Front-End Django DevelopersBest Practices for Front-End Django Developers
Best Practices for Front-End Django DevelopersChristine Cheung
 
Realtime Apps with Django
Realtime Apps with DjangoRealtime Apps with Django
Realtime Apps with DjangoRenyi Khor
 
Functional testing your Grails app with GEB
Functional testing your Grails app with GEBFunctional testing your Grails app with GEB
Functional testing your Grails app with GEBGR8Conf
 
Taming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and GebTaming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and GebC4Media
 
A Closer Look At React Native
A Closer Look At React NativeA Closer Look At React Native
A Closer Look At React NativeIan Wang
 
DevQA: make your testers happier with Groovy, Spock and Geb
DevQA: make your testers happier with Groovy, Spock and GebDevQA: make your testers happier with Groovy, Spock and Geb
DevQA: make your testers happier with Groovy, Spock and GebAlvaro Sanchez-Mariscal
 
Tek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJSTek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJSPablo Godel
 
HTML5 for PHP Developers - IPC
HTML5 for PHP Developers - IPCHTML5 for PHP Developers - IPC
HTML5 for PHP Developers - IPCMayflower GmbH
 
What's This React Native Thing I Keep Hearing About?
What's This React Native Thing I Keep Hearing About?What's This React Native Thing I Keep Hearing About?
What's This React Native Thing I Keep Hearing About?Evan Stone
 
Ratpack - Classy and Compact Groovy Web Apps
Ratpack - Classy and Compact Groovy Web AppsRatpack - Classy and Compact Groovy Web Apps
Ratpack - Classy and Compact Groovy Web AppsJames Williams
 
Integrating react in django while staying sane and happy
Integrating react in django while staying sane and happyIntegrating react in django while staying sane and happy
Integrating react in django while staying sane and happyFröjd Interactive
 
Writing plugins for Kate and enhancing Kate
Writing plugins for Kate and enhancing KateWriting plugins for Kate and enhancing Kate
Writing plugins for Kate and enhancing KateAbhishek Patil
 
From zero to hero with React Native!
From zero to hero with React Native!From zero to hero with React Native!
From zero to hero with React Native!Commit University
 

What's hot (19)

Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Django Introduction & Tutorial
Django Introduction & TutorialDjango Introduction & Tutorial
Django Introduction & Tutorial
 
Introduction to Web Frameworks
Introduction to Web FrameworksIntroduction to Web Frameworks
Introduction to Web Frameworks
 
Best Practices for Front-End Django Developers
Best Practices for Front-End Django DevelopersBest Practices for Front-End Django Developers
Best Practices for Front-End Django Developers
 
Realtime Apps with Django
Realtime Apps with DjangoRealtime Apps with Django
Realtime Apps with Django
 
Django
DjangoDjango
Django
 
Functional testing your Grails app with GEB
Functional testing your Grails app with GEBFunctional testing your Grails app with GEB
Functional testing your Grails app with GEB
 
Taming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and GebTaming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and Geb
 
BDD, Behat & Drupal
BDD, Behat & DrupalBDD, Behat & Drupal
BDD, Behat & Drupal
 
A Closer Look At React Native
A Closer Look At React NativeA Closer Look At React Native
A Closer Look At React Native
 
DevQA: make your testers happier with Groovy, Spock and Geb
DevQA: make your testers happier with Groovy, Spock and GebDevQA: make your testers happier with Groovy, Spock and Geb
DevQA: make your testers happier with Groovy, Spock and Geb
 
Tek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJSTek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJS
 
HTML5 for PHP Developers - IPC
HTML5 for PHP Developers - IPCHTML5 for PHP Developers - IPC
HTML5 for PHP Developers - IPC
 
Geb with spock
Geb with spockGeb with spock
Geb with spock
 
What's This React Native Thing I Keep Hearing About?
What's This React Native Thing I Keep Hearing About?What's This React Native Thing I Keep Hearing About?
What's This React Native Thing I Keep Hearing About?
 
Ratpack - Classy and Compact Groovy Web Apps
Ratpack - Classy and Compact Groovy Web AppsRatpack - Classy and Compact Groovy Web Apps
Ratpack - Classy and Compact Groovy Web Apps
 
Integrating react in django while staying sane and happy
Integrating react in django while staying sane and happyIntegrating react in django while staying sane and happy
Integrating react in django while staying sane and happy
 
Writing plugins for Kate and enhancing Kate
Writing plugins for Kate and enhancing KateWriting plugins for Kate and enhancing Kate
Writing plugins for Kate and enhancing Kate
 
From zero to hero with React Native!
From zero to hero with React Native!From zero to hero with React Native!
From zero to hero with React Native!
 

Viewers also liked

Starters with Django
Starters with Django Starters with Django
Starters with Django BeDjango
 
Web Development with Python and Django
Web Development with Python and DjangoWeb Development with Python and Django
Web Development with Python and DjangoMichael Pirnat
 
Django Forms: Best Practices, Tips, Tricks
Django Forms: Best Practices, Tips, TricksDjango Forms: Best Practices, Tips, Tricks
Django Forms: Best Practices, Tips, TricksShawn Rider
 
Advanced Django Forms Usage
Advanced Django Forms UsageAdvanced Django Forms Usage
Advanced Django Forms UsageDaniel Greenfeld
 
Django - Python MVC Framework
Django - Python MVC FrameworkDjango - Python MVC Framework
Django - Python MVC FrameworkBala Kumar
 
12 tips on Django Best Practices
12 tips on Django Best Practices12 tips on Django Best Practices
12 tips on Django Best PracticesDavid Arcos
 
The Best (and Worst) of Django
The Best (and Worst) of DjangoThe Best (and Worst) of Django
The Best (and Worst) of DjangoJacob Kaplan-Moss
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesMatt Harrison
 
Тайм-менеджмент для иррационалов: как неорганизованным людям управлять делами
Тайм-менеджмент для иррационалов: как неорганизованным людям управлять деламиТайм-менеджмент для иррационалов: как неорганизованным людям управлять делами
Тайм-менеджмент для иррационалов: как неорганизованным людям управлять деламиArtem Polyanskiy
 
Django Overview
Django OverviewDjango Overview
Django OverviewBrian Tol
 
Django tech-talk
Django tech-talkDjango tech-talk
Django tech-talkdtdannen
 
Jumpstart Django
Jumpstart DjangoJumpstart Django
Jumpstart Djangoryates
 
Django Admin (Python meeutp)
Django Admin (Python meeutp)Django Admin (Python meeutp)
Django Admin (Python meeutp)Ines Jelovac
 

Viewers also liked (20)

Django in the Real World
Django in the Real WorldDjango in the Real World
Django in the Real World
 
Starters with Django
Starters with Django Starters with Django
Starters with Django
 
Web Development with Python and Django
Web Development with Python and DjangoWeb Development with Python and Django
Web Development with Python and Django
 
Advanced Django
Advanced DjangoAdvanced Django
Advanced Django
 
Django In The Real World
Django In The Real WorldDjango In The Real World
Django In The Real World
 
Free django
Free djangoFree django
Free django
 
Django Forms: Best Practices, Tips, Tricks
Django Forms: Best Practices, Tips, TricksDjango Forms: Best Practices, Tips, Tricks
Django Forms: Best Practices, Tips, Tricks
 
Advanced Django Forms Usage
Advanced Django Forms UsageAdvanced Django Forms Usage
Advanced Django Forms Usage
 
Django - Python MVC Framework
Django - Python MVC FrameworkDjango - Python MVC Framework
Django - Python MVC Framework
 
12 tips on Django Best Practices
12 tips on Django Best Practices12 tips on Django Best Practices
12 tips on Django Best Practices
 
The Best (and Worst) of Django
The Best (and Worst) of DjangoThe Best (and Worst) of Django
The Best (and Worst) of Django
 
Python/Django Training
Python/Django TrainingPython/Django Training
Python/Django Training
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
Django Deployment
Django DeploymentDjango Deployment
Django Deployment
 
Тайм-менеджмент для иррационалов: как неорганизованным людям управлять делами
Тайм-менеджмент для иррационалов: как неорганизованным людям управлять деламиТайм-менеджмент для иррационалов: как неорганизованным людям управлять делами
Тайм-менеджмент для иррационалов: как неорганизованным людям управлять делами
 
Why Django
Why DjangoWhy Django
Why Django
 
Django Overview
Django OverviewDjango Overview
Django Overview
 
Django tech-talk
Django tech-talkDjango tech-talk
Django tech-talk
 
Jumpstart Django
Jumpstart DjangoJumpstart Django
Jumpstart Django
 
Django Admin (Python meeutp)
Django Admin (Python meeutp)Django Admin (Python meeutp)
Django Admin (Python meeutp)
 

Similar to Getting Started With Django

Intro To Django
Intro To DjangoIntro To Django
Intro To DjangoUdi Bauman
 
Learn Django Tips, Tricks & Techniques for Developers
Learn Django Tips, Tricks & Techniques for DevelopersLearn Django Tips, Tricks & Techniques for Developers
Learn Django Tips, Tricks & Techniques for DevelopersMars Devs
 
Django Tutorial_ Let’s take a deep dive into Django’s web framework.pdf
Django Tutorial_ Let’s take a deep dive into Django’s web framework.pdfDjango Tutorial_ Let’s take a deep dive into Django’s web framework.pdf
Django Tutorial_ Let’s take a deep dive into Django’s web framework.pdfSudhanshiBakre1
 
Advantages Of Using Django Framework To Build Scalable.pdf
Advantages Of Using Django Framework To Build Scalable.pdfAdvantages Of Using Django Framework To Build Scalable.pdf
Advantages Of Using Django Framework To Build Scalable.pdfMindfire LLC
 
Concepts and applications of Django.pptx
Concepts and applications of Django.pptxConcepts and applications of Django.pptx
Concepts and applications of Django.pptxsushmitjivtode4
 
Django Workflow and Architecture
Django Workflow and ArchitectureDjango Workflow and Architecture
Django Workflow and ArchitectureAndolasoft Inc
 
Top 13 best front end web development tools to consider in 2021
Top 13 best front end web development tools to consider in 2021Top 13 best front end web development tools to consider in 2021
Top 13 best front end web development tools to consider in 2021Samaritan InfoTech
 
Hiring Django Developers for Success.pdf
Hiring Django Developers for Success.pdfHiring Django Developers for Success.pdf
Hiring Django Developers for Success.pdfAIS Technolabs Pvt Ltd
 
Django Article V0
Django Article V0Django Article V0
Django Article V0Udi Bauman
 
0506-django-web-framework-for-python.pdf
0506-django-web-framework-for-python.pdf0506-django-web-framework-for-python.pdf
0506-django-web-framework-for-python.pdfradhianiedjan1
 
Django Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python DevelopersDjango Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python DevelopersRosario Renga
 
quantum_leap_angularjs_tools_redefining_development_in_2023.pptx
quantum_leap_angularjs_tools_redefining_development_in_2023.pptxquantum_leap_angularjs_tools_redefining_development_in_2023.pptx
quantum_leap_angularjs_tools_redefining_development_in_2023.pptxsarah david
 
GUI toolkits comparison for python
GUI toolkits comparison for pythonGUI toolkits comparison for python
GUI toolkits comparison for pythonDarren Su
 
What is Django | Django Tutorial for Beginners | Python Django Training | Edu...
What is Django | Django Tutorial for Beginners | Python Django Training | Edu...What is Django | Django Tutorial for Beginners | Python Django Training | Edu...
What is Django | Django Tutorial for Beginners | Python Django Training | Edu...Edureka!
 
10 Advantages Of Using Django For Web Development.pdf
10 Advantages Of Using Django For Web Development.pdf10 Advantages Of Using Django For Web Development.pdf
10 Advantages Of Using Django For Web Development.pdfAppdeveloper10
 

Similar to Getting Started With Django (20)

Intro To Django
Intro To DjangoIntro To Django
Intro To Django
 
Learn Django Tips, Tricks & Techniques for Developers
Learn Django Tips, Tricks & Techniques for DevelopersLearn Django Tips, Tricks & Techniques for Developers
Learn Django Tips, Tricks & Techniques for Developers
 
Django Tutorial_ Let’s take a deep dive into Django’s web framework.pdf
Django Tutorial_ Let’s take a deep dive into Django’s web framework.pdfDjango Tutorial_ Let’s take a deep dive into Django’s web framework.pdf
Django Tutorial_ Let’s take a deep dive into Django’s web framework.pdf
 
Django
DjangoDjango
Django
 
Advantages Of Using Django Framework To Build Scalable.pdf
Advantages Of Using Django Framework To Build Scalable.pdfAdvantages Of Using Django Framework To Build Scalable.pdf
Advantages Of Using Django Framework To Build Scalable.pdf
 
wt mod3.pdf
wt mod3.pdfwt mod3.pdf
wt mod3.pdf
 
Concepts and applications of Django.pptx
Concepts and applications of Django.pptxConcepts and applications of Django.pptx
Concepts and applications of Django.pptx
 
Django Workflow and Architecture
Django Workflow and ArchitectureDjango Workflow and Architecture
Django Workflow and Architecture
 
django
djangodjango
django
 
Top 13 best front end web development tools to consider in 2021
Top 13 best front end web development tools to consider in 2021Top 13 best front end web development tools to consider in 2021
Top 13 best front end web development tools to consider in 2021
 
Hiring Django Developers for Success.pdf
Hiring Django Developers for Success.pdfHiring Django Developers for Success.pdf
Hiring Django Developers for Success.pdf
 
Django Article V0
Django Article V0Django Article V0
Django Article V0
 
0506-django-web-framework-for-python.pdf
0506-django-web-framework-for-python.pdf0506-django-web-framework-for-python.pdf
0506-django-web-framework-for-python.pdf
 
Django framework
Django frameworkDjango framework
Django framework
 
Django Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python DevelopersDjango Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python Developers
 
quantum_leap_angularjs_tools_redefining_development_in_2023.pptx
quantum_leap_angularjs_tools_redefining_development_in_2023.pptxquantum_leap_angularjs_tools_redefining_development_in_2023.pptx
quantum_leap_angularjs_tools_redefining_development_in_2023.pptx
 
GUI toolkits comparison for python
GUI toolkits comparison for pythonGUI toolkits comparison for python
GUI toolkits comparison for python
 
What is Django | Django Tutorial for Beginners | Python Django Training | Edu...
What is Django | Django Tutorial for Beginners | Python Django Training | Edu...What is Django | Django Tutorial for Beginners | Python Django Training | Edu...
What is Django | Django Tutorial for Beginners | Python Django Training | Edu...
 
10 Advantages Of Using Django For Web Development.pdf
10 Advantages Of Using Django For Web Development.pdf10 Advantages Of Using Django For Web Development.pdf
10 Advantages Of Using Django For Web Development.pdf
 
API-First Design and Django
API-First Design and DjangoAPI-First Design and Django
API-First Design and Django
 

Recently uploaded

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 

Recently uploaded (20)

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 

Getting Started With Django

  • 1. Web Design and Developer Conference 2008 Jeff Croft October 16th, 2008 The Web Design Conference, Orlando Introduction to Django
  • 2. Web Design and Developer Conference 2008 Introduction
  • 3. Web Design and Developer Conference 2008 I’m Jeff Croft.
  • 4. Web Design and Developer Conference 2008 What do I do? • Design and develop web and mobile sites and applications for Blue Flavor and its clients. • Use and evangelize Django, an open source, Python-based web application framework that originated at The World Company, my former employer. • Write about web design, Django, Mac nerdery, and whatever else strikes my fancy at jeffcroft.com.
  • 5. Web Design and Developer Conference 2008 What is Django?
  • 6. Web Design and Developer Conference 2008 From the horse’s mouth: “ Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.” djangoproject.com
  • 7. Web Design and Developer Conference 2008 “...high level Python web framework...” • A web framework attempts to abstract routine development tasks by providing shortcuts. • For example: dealing with the details of HTTP, connecting to a database, managing users and groups, generating feeds, etc. • However, a good web framework shouldn’t get in your way if you want or need to work at this lower level. You should always be able to “step down a level.” • Django is built on Python, an established high-level language used heavily at Google, Yahoo, CERN, and NASA. • Django makes a point of not changing anything about the way Python works. If you learn Django, you’re learning Python—and vice-versa.
  • 8. Web Design and Developer Conference 2008 “...encourages rapid development...” • Saving you time is one of Django’s primary goals. • Django was created in the fast-paced news world, and was designed around a real-world need to develop sites in a matter of hours, not days or weeks.
  • 9. Web Design and Developer Conference 2008 “...clean, pragmatic design.” • Django is opinionated software. It firmly believes there is a “right” way to do many things. • It tries to make it as easy as possible to do things the “right” way. Django helps you subscribe to best practices by making them simple.
  • 10. Web Design and Developer Conference 2008 A bit of Django history • Created in 2003 at The World Company in Lawrence, KS, by Adrian Holovaty and Simon Willison. Originally, it was call “The CMS”, and was a project to build a CMS for newspapers. • As time passed, they abstracted the underlying “stuff to help you build web apps” from the higher level “stuff that makes running an online newspaper easier.” The two became Django (open source) and Ellington (commerical), respectively. • Django was released as open source software in the summer of 2005. Since then, it has really taken off, and is in use at many major corporations, include Google. • Predictably, Django became extremely popular in the journalism world. In 2007, there were 14 Django-powered sites among the finalists in 11 categories for the Digital Edge online journalism awards. Most of these were running Ellington.
  • 11. Web Design and Developer Conference 2008 Continued... • The core Django developers had several big changes they wanted to make that would introduce backwards- incompatibilities for developers who’d built sites on Django. They decided to make these changes before giving Django the 1.0 version number. As such, the time between Django’s original release and it’s 1.0 release was...well, ridiculous. • Django 1.0 was released on September 2nd, 2008! • The good news for you: there should be no significant backwards incompatible changes from here on out. The code you will learn today will be valid Django code for a very long time.
  • 12. Web Design and Developer Conference 2008 Projects and apps
  • 13. Web Design and Developer Conference 2008 Projects in Django • A project is a configuration for an instance of Django. • It contains things like database settings, filesystem paths, and so forth. It may also contain settings for applications (more on applications soon). • It also contains a list of the applications your project will be using. • Usually, a project is associated with a single website—each website has a project that controls it. There are cases where a single project can drive multiple websites, but that’s more advanced and not for you to worry about right now.
  • 14. Web Design and Developer Conference 2008 Apps in Django • An “app” in Django is a collection of code that makes up some sort of functionality for your website. • A typical project will use several apps. • Apps can be reused between projects (this is one of Django’s coolest features — more on it later). • Deciding what is an “app” can be tricky. For example, should you make an app that handles your blog and comments, or should you make two apps (one for blog, one for comments)? • Generally, it’s best to make two apps. But don’t worry about this too much right now. You can always refactor your code into multiple apps later, when you start to see how it might be beneficial.
  • 15. Web Design and Developer Conference 2008 Apps are “pluggable” • One of Django’s greatest strengths is that a single app can be reused in many projects. • This has fostered a great community around building reusable apps that are made available for you to just “plug in” to your project.
  • 16. Web Design and Developer Conference 2008 Let’s say you have three sites: • First, you have a personal site. It includes a blog with tags and comments, a photo gallery, some “static” pages, and contact form. • You also have a Digg-style social news site. It contains stories with tags and comments, the ability for users to vote on stories, some “static” pages, and a contact form. • You also have a movie review site. It contains a database of movies with tags, your reviews with tags and comments, the ability for users to vote on movies, some “static” pages, and a contact form.
  • 17. Web Design and Developer Conference 2008 There’s some overlap here. • All three sites have comments, “static” pages, and a contact form. • Two sites have blogs and photo galleries. Two sites also allow users to vote on items. • Ideally, you build generic apps for these functions, so that they can work across all three sites. Clearly, this is better for maintainability.
  • 18. Web Design and Developer Conference 2008 These already exist. • Django comes with a comments app. It allows users to comment on any sort of item in your database (blog entry, story, movie review, etc.) • Django also comes with an app for handling “static” pages (simple pages with content stored in the database). • django-tagging (http://code.google.com/p/django-tagging/) allows you to easily add tag functionality to any sort of item in your database. • django-voting (http://code.google.com/p/django-voting/) allows users to vote on any sort of item in your database. • django-contact-form (http://code.google.com/p/django- contact-form/) provides basic contact form functionality. • There are several available blog and photo gallery applications.
  • 19. Web Design and Developer Conference 2008 django.contrib • Django is distributed with several of these reusable apps. They’re all optional, but available for use in your project if you want them. Some of them are: • django.contrib.admin — Provides an automatic admin interface • django.contrib.auth — Provides users and groups • django.contrib.comments — Provides commenting functionality • django.contrib.flatpages — Handles simple, “static” pages • django.contrib.syndication — Makes creating feeds easy
  • 20. Web Design and Developer Conference 2008 More examples • All of the following are available on Google Code (as well as a ton of others — these are just a few I thought were useful): • django-pagination • django-mailfriend • django-messages • django-forum • django-profiles • django-friends • django-registration • django-authopenid • django-basic-apps
  • 21. Web Design and Developer Conference 2008 Installing an app in a project • In order to use your app in a project, you need to place a reference to it in the INSTALLED_APPS list in your settings.py file. INSTALLED_APPS = ( quot;django.contrib.authquot;, quot;django.contrib.contenttypesquot;, quot;django.contrib.sessionsquot;, quot;django.contrib.sitesquot;, quot;registrationquot;, quot;paginationquot;, )
  • 22. Web Design and Developer Conference 2008 Models
  • 23. Web Design and Developer Conference 2008 What are models? • A model is a description of a particular type of data in your application. • Your models define what types of data will be available in your application and what fields and attributes those objects have. • In Django, model definitions also contain metadata about your models. • In Django, models map one-to-one to database tables.
  • 24. Web Design and Developer Conference 2008 Typically, you’d do this: CREATE TABLE quot;people_personquot; ( quot;idquot; integer NOT NULL PRIMARY KEY, quot;first_namequot; varchar(300) NOT NULL, quot;last_namequot; varchar(300) NOT NULL, quot;bioquot; text NOT NULL, );
  • 25. Web Design and Developer Conference 2008 But not in Django. • SQL is not consistent from database to database. Django projects should easily port from one database environment to another without having to change your code. Django models work with any database Django supports. • If you store your models in the database, it becomes difficult to keep them under version control. • Your application will need to work with the model regardless of how you created it. If you created it in SQL, you’d need to recreate it in Python so your application can work with it. Thus, Django just has you create models in Python from the start, and handles the SQL for you. • Writing Python is fun! Well, at least it’s a lot more fun than writing SQL. If you ask me, anyway. :)
  • 26. Web Design and Developer Conference 2008 The same model, in Django: from django.db import models class Person(models.Model): first_name = models.CharField(max_length=300) last_name = models.CharField(max_length=300) bio = models.TextField() def __unicode__(self): return (%s %s) % (self.first_name, self.last_name) class Meta: ordering = [quot;last_namequot;] verbose_name = quot;Personquot; verbose_name_plural = quot;Peoplequot;
  • 27. Web Design and Developer Conference 2008 The same model, in Django: from django.db import models class Person(models.Model): first_name = models.CharField(max_length=300) last_name = models.CharField(max_length=300) bio = models.TextField() def __unicode__(self): return (%s %s) % (self.first_name, self.last_name) class Meta: ordering = [quot;last_namequot;] verbose_name = quot;Personquot; verbose_name_plural = quot;Peoplequot;
  • 28. Web Design and Developer Conference 2008 Models can contain relationships from django.db import models class Person(models.Model): first_name = models.CharField(max_length=300) last_name = models.CharField(max_length=300) bio = models.TextField() ... class Article(models.Model): title = models.CharField(max_length=300) date = models.DateTimeField() author = models.ForeignKey(Person,related_name= quot;articlesquot;)
  • 29. Web Design and Developer Conference 2008 Syncing your database • Once you’ve created models, Django will run the necessary SQL to install them into the database for you. You just need to run the manage.py command “syncdb” $ ./manage.py syncdb Creating table my_first_model Creating table my_second_model ... • This will install all models for any app in your project’s INSTALLED_APPS setting.
  • 30. Web Design and Developer Conference 2008 Django’s database API • Once you’ve installed your models, Django provides a high- level API for working with them. • The API provides functions for creating, retrieving, changing, and deleting objects in the database — and a lot more.
  • 31. Web Design and Developer Conference 2008 Creating an object • Assuming the “Person” model I showed earlier, you can create a new person in the database as follows: p = Person( first_name = quot;Jeffquot;, last_name = quot;Croftquot;, bio = quot;Jeff Croft is awesome.quot; ) p.save() • Note the explicit .save() method each model gets. This is an important bit of Django philosophy: Django never hits the database until you explicitly tell it to. Thus, the .save() call is required.
  • 32. Web Design and Developer Conference 2008 Retrieving objects • Each model gets a member called “objects,” which is used for data retrieval. A few examples: Person.objects.all() Person.objects.get(id=10) Person.objects.filter(first_name=quot;jeffquot;) Person.objects.exclude(first_name__contains=quot;jequot;) Person.objects.all().order_by(quot;first_namequot;) Article.objects.filter(author__first_name=quot;jeffquot;) • You can chain these things. You can do, for example: Person.objects.filter().exclude().order_by() • The Django database API is very powerful, and I’ve personally never come across a situation where it couldn’t do what I needed it to do. But, if there is a case when you need to write SQL directly, Django will let you do that.
  • 33. Web Design and Developer Conference 2008 Using relationships p = Person.objects.get(first_name=quot;Jeffquot;, last_name=quot;Croftquot;) p.articles.all() p.articles.filter(title__contains=quot;djangoquot;) a = Article.objects.get(title=quot;Django is greatquot;) a.author a.author.first_name
  • 34. Web Design and Developer Conference 2008 Editing an object p = Person.objects.get(first_name=quot;Jeffquot;, last_name=quot;Croftquot;) p.bio = quot;No, really. Jeff Croft is totally awesome.quot; p.save()
  • 35. Web Design and Developer Conference 2008 Deleting an object p = Person.objects.get(first_name=quot;Jeffquot;, last_name=quot;Croftquot;) p.delete()
  • 36. Web Design and Developer Conference 2008 Lazy QuerySets • When you pull items from the database, Django calls the result a “QuerySet.” It’s more or less a Python list with some added functionality. QuerySets are lazy. That is to say, they only hit the database when they're evaluated. For example: people = Person.objects.filter(first_name=quot;jeffquot;) people = people.exclude(last_name__contains=quot;croftquot;) people = people.order_by(quot;last_namequot;) print people # Database hit • This code executes exactly one database query.
  • 37. Web Design and Developer Conference 2008 Django admin
  • 38. Web Design and Developer Conference 2008 What is the Django admin? • The Django admin is one of the afore mentioned optional “contrib” apps that are distributed with Django itself. • It provides a powerful, customizable, production-ready interface to creating, updating, and deleting instances of your models. • It includes data entry validation, user and group permissions, and a log of user activity in the Admin. • It is designed for regular users. It’s not like phpMyAdmin or other database administration tools. It was built at a newspaper so that editors and producers could enter and update content. • The Django admin works off your models alone. Even if your app is not done, once you have the models in place, you can start working with the Django admin.
  • 39. Web Design and Developer Conference 2008
  • 40. Web Design and Developer Conference 2008
  • 41. Web Design and Developer Conference 2008
  • 42. Web Design and Developer Conference 2008 Why? And what if I don’t want it? • Writing admin interfaces sucks. It’s boring, repetitive, and just usually not that much fun. • The admin interface is entirely optional. It’s just a Django app you plug into your project like any other. If you don’t want it, you just don’t plug it in. • If you want or need to write your own administrative interface for your project, you can certainly do so.
  • 43. Web Design and Developer Conference 2008 Views
  • 44. Web Design and Developer Conference 2008 What are views? • A view is (generally) a “type” of web page within your application. • A view (generally) performs a specific function and renders a specific template. • A view is simply a Python function that takes an HTTP request and returns an HTTP response. • View functions can live anywhere in your Python path, but are typically put into the views.py file for your application.
  • 45. Web Design and Developer Conference 2008 A view is not “how it looks.” • In most cases, the view is responsible for pulling together all the information that’s available at a given URL. It may look up information in the database, perform some kind of processing on data, process input from a form, send an e-mail, etc. • Since a view is just a Python function, it can really do anything you want it to. The only rules are that it must take an HTTP request and return an HTTP response. • The view shouldn’t be outputting (X)HTML directly. Rather, it will render through a template, which is where the (X)HTML is structured. We’ll deal with templates later.
  • 46. Web Design and Developer Conference 2008 A simple first view: from django.http import HttpResponse def homepage(request): return HttpResponse(quot;Welcome to the homepage!quot;)
  • 47. Web Design and Developer Conference 2008 A less simple view: from django.shortcuts import render_to_response from myproject.myapp import Person def people_list(request): people = Person.objects.all() return render_to_response( quot;people/list.htmlquot;, {quot;people_listquot;: people}, )
  • 48. Web Design and Developer Conference 2008 Another example: from django.shortcuts import render_to_response, get_object_or_404 from myproject.myapp import Person def person_detail(request, lastname): person = get_object_or_404(Person, last_name=lastname) return render_to_response{ quot;people/detail.htmlquot;, {quot;personquot;: person}, }
  • 49. Web Design and Developer Conference 2008 Generic views
  • 50. Web Design and Developer Conference 2008 What are generic views? • Generic views are a set of regular Django views that are distributed with Django and cover a large variety of standard use cases. • They save you time, by making you not have to write views in “typical” situations. You can just use the included generic views, instead. • In many cases, generic views are all (or most of what) you need. Simple sites often use exclusively generic views. At the newspapers I worked for, generic views covered around 85% of the pages on the site.
  • 51. Web Design and Developer Conference 2008 Django provides: • django.views.generic.simple • django.views.generic.date_based • django.views.generic.list_detail • django.views.generic.create_update
  • 52. Web Design and Developer Conference 2008 URLconfs
  • 53. Web Design and Developer Conference 2008 What are URLconfs? • In Django, you explicitly define which URLs map to which functions. The URLs are not implicit from the filesystem, from the view name, or from anything else. You must consciously think about what your URLs should look like and how they should work. • A “URLconf”, as Django people call them, is a mapping of URLs to view functions. For example, if someone visiting the URL http://yoursite.com, the URL conf points them to the view function you’ve written for your homepage. If they go to http:// yoursite.com/blog, the URL conf points them to the view function you’ve written for your blog index. And so forth. • Django uses regular expressions to match URLs. • URLconfs can live anywhere in your Python path, but typically each application will have a urls.py file outlining the URLs it provides, and each project will also have a urls.py file.
  • 54. Web Design and Developer Conference 2008 Why? Because these are ugly. • page.php • script.cgi?pageid=144 • StoryPage.aspx • 0,2097,1-1-30-72-407-4752,00.html • /stories/147/ • URLs are part of your application design. You should think about them as a key part of the interface to your site and design them with the same level of attention you’d design any other interface element.
  • 55. Web Design and Developer Conference 2008 Django URLs look more like this: • /stories/ • /stories/2008/apr/20/michelles-birthday/ • /categories/birthdays/ • Really, Django URLs can look any way you want them to—even the ugly ways in the previous slide. But, this sort of scheme is typical.
  • 56. Web Design and Developer Conference 2008 Templates
  • 57. Web Design and Developer Conference 2008 What are templates? • If a view describes what data is on a page, then a template defines how that data is presented as HTML (usually). • While Django’s template language is most commonly used to generate (X)HTML, it can actually be used to create any text- based file format (e-mail, RSS, CSV, XML, YAML, etc.). • Django’s template language was designed to be usable by designers and HTML authors. It should not require a programmer to operate it.
  • 58. Web Design and Developer Conference 2008 An example template <!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;> <html lang=quot;enquot;> <head><title>People</title></head> <body> <h1>People ({{ people_list|length }} total)</h1> <ul> {% for p in person_list %} <li> <a href=quot;{{ p.last_name }}/quot;> {{ p.first_name }} {{ p.last_name }} </a> </li> {% endfor %} </ul> </body> </html>
  • 59. Web Design and Developer Conference 2008 Three main template components • Variables: Remember how our view provided a “context” to the template? That context is the set of variables our template has access to. Variables are used like this: {{ variable_name }} • Tags: Tags are more complex than variables. Some create text in the template, some perform loops or logic, some add data to the current template context, and some do other stuff. A tag can do virtually anything. Tags may take arguments. Tags are used like this: {% tag_name %} • Filters: Filters can be thought of like a pipe, in UNIX. You pass something into them, and they return something different. They can be strung together, too. Many filters reformat a variable. For example, the “date” filter defines how a date variable will be outputted. Filters are used like this {{ variable|filter_name }}
  • 60. Web Design and Developer Conference 2008 Variables <!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;> <html lang=quot;enquot;> <head><title>People</title></head> <body> <h1>{{ person.first_name }} {{ person.last_name }}</h1> <p>{{ person.bio }}</p> </body> </html>
  • 61. Web Design and Developer Conference 2008 Variables <!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;> <html lang=quot;enquot;> <head><title>People</title></head> <body> <h1>{{ person.first_name }} {{ person.last_name }}</h1> <p>{{ person.bio }}</p> </body> </html>
  • 62. Web Design and Developer Conference 2008 Filters <!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;> <html lang=quot;enquot;> <head><title>People</title></head> <body> <h1>People ({{ people_list|length }} total)</h1> <ul> {% for p in person_list %} <li> <a href=quot;{{ p.last_name }}/quot;> {{ p.first_name|capfirst }}{{ p.last_name|striptags }} </a> </li> {% endfor %} </ul> </body> </html>
  • 63. Web Design and Developer Conference 2008 Filters <!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;> <html lang=quot;enquot;> <head><title>People</title></head> <body> <h1>People ({{ people_list|length }} total)</h1> <ul> {% for p in person_list %} <li> <a href=quot;{{ p.last_name }}/quot;> {{ p.first_name|capfirst }}{{ p.last_name|striptags }} </a> </li> {% endfor %} </ul> </body> </html>
  • 64. Web Design and Developer Conference 2008 Tags <!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;> <html lang=quot;enquot;> <head><title>People</title></head> <body> <h1>People ({{ people_list|length }} total)</h1> <ul> {% for p in person_list %} <li> <a href=quot;{{ p.last_name }}/quot;> {{ p.first_name }}{{ p.last_name }} </a> </li> {% endfor %} </ul> </body> </html>
  • 65. Web Design and Developer Conference 2008 Tags <!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;> <html lang=quot;enquot;> <head><title>People</title></head> <body> <h1>People ({{ people_list|length }} total)</h1> <ul> {% for p in person_list %} <li> <a href=quot;{{ p.last_name }}/quot;> {{ p.first_name }}{{ p.last_name }} </a> </li> {% endfor %} </ul> </body> </html>
  • 66. Web Design and Developer Conference 2008 Template inheritance • Django’s template language includes an inheritance mechanism that is both extremely powerful and sort of hard to get your head around at first. • You might think of it as an updated (if backwards) take on “includes,” which you may have used in the past (SSI, PHP includes, etc.) • The core problem inheritance is trying to solve is one of repeated code. Ideally, you Don’t Repeat Yourself (DRY), and any time you need to make changes to template code, you only have to do it in one place.
  • 67. Web Design and Developer Conference 2008 “Blocks” • Django’s template inheritance centers around the idea of “blocks”. A template can define “blocks”—essentially chunks another template can override. • A typical setup is to have a “base” template which provides a bunch of empty blocks, and then “child” templates which fill them. In Django parlance, child templates “extend” other templates. • Extension doesn’t have to be only two level: you could have a “base” template for your site, plus a template for a section of your site which extends the base, plus a template that extends the section template. And so on.
  • 68. Web Design and Developer Conference 2008 A “base” template base.html <!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;> <html lang=quot;enquot;> <head><title>{% block title %}{% endblock %}</title></head> <body> <div id=”content”> {% block content %} <h1>Welcome to our site!</h1> {% endblock %} </div> <div id=”footer”> {% block footer %} <p>Copyright 2008 Jeff Croft</p> {% endblock %} </div> </body> </html>
  • 69. Web Design and Developer Conference 2008 “Blocks” <!DOCTYPE HTML PUBLIC quot;-//W3C//DTD HTML 4.01//ENquot;> <html lang=quot;enquot;> <head><title>{% block title %}{% endblock %}</title></head> <body> <div id=”content”> {% block content %} <h1>Welcome to our site!</h1> {% endblock %} </div> <div id=”footer”> {% block footer %} <p>Copyright 2008 Jeff Croft</p> {% endblock %} </div> </body> </html>
  • 70. Web Design and Developer Conference 2008 A child template person.html {% extends “base.html” %} {% block title %} {{ person.first_name }}{{ person.last_name }} {% endblock %} {% block content %} <h1>About {{ person.first_name }}{{ person.last_name }}</h1> {% endblock %}
  • 71. Web Design and Developer Conference 2008 A child template person.html {% extends “base.html” %} {% block title %} {{ person.first_name }}{{ person.last_name }} {% endblock %} {% block content %} <h1>About {{ person.first_name }}{{ person.last_name }}</h1> {% endblock %}
  • 72. Web Design and Developer Conference 2008 The beauty of inheritance • Besides keeping everything DRY, one of the biggest reasons to make good use of inheritance is for redesigns: often, replacing the base.html template is all you have to do to get a totally new layout, look, and feel.
  • 73. Web Design and Developer Conference 2008 A note on tags and filters • Django is distributed with a vast library of template tags and filters. • As you get more advanced, though, you may wish for a template tag that doesn’t exist. • Template tags and filters are just Python functions, so there’s nothing to stop you from writing your own. In fact, most Django apps provide some template tags (along with their models, views, and urls). • To load a non-built-in set of tags (such as those an application might provide), use the “load” tag: {% load blog_tags %}
  • 74. Web Design and Developer Conference 2008 What else is in Django?
  • 75. Web Design and Developer Conference 2008 Some of the things we didn’t cover: • Forms: Handles HTML form display, data processing (validation) and redisplay. • Caching: Flexible caching at any level, using memcached, database, local memory, or filesystem caching. • Internationalization: Full support for internationalization of text. • Sessions: Full support for anonymous, cookie-based sessions. • Testing: Full testing suite included. • Middleware: a framework of hooks into Django’s request/ response processing. Allow for global altering of Django’s input and/or output.
  • 76. Web Design and Developer Conference 2008 Okay, go write some code!
  • 77. Web Design and Developer Conference 2008 (or ask me questions.)
  • 78. Web Design and Developer Conference 2008 Thanks, yo.