2005-07-15 15:57:46 +00:00
|
|
|
==================
|
|
|
|
Django at a glance
|
|
|
|
==================
|
2005-07-15 15:31:20 +00:00
|
|
|
|
|
|
|
Because Django was developed in a fast-paced newsroom environment, it was
|
|
|
|
designed to make common Web-development tasks fast and easy. Here's an informal
|
|
|
|
overview of how to write a database-driven Web app with Django.
|
|
|
|
|
|
|
|
The goal of this document is to give you enough technical specifics to
|
|
|
|
understand how Django works, but this isn't intended to be a tutorial or
|
|
|
|
reference. Please see our more-detailed Django documentation_ when you're ready
|
|
|
|
to start a project.
|
|
|
|
|
|
|
|
.. _documentation: http://www.djangoproject.com/documentation/
|
|
|
|
|
|
|
|
Design your model
|
|
|
|
=================
|
|
|
|
|
|
|
|
Start by describing your database layout in Python code. Django's data-model API
|
2005-07-15 15:57:46 +00:00
|
|
|
offers many rich ways of representing your models -- so far, it's been
|
2005-07-15 15:31:20 +00:00
|
|
|
solving two years' worth of database-schema problems. Here's a quick example::
|
|
|
|
|
|
|
|
class Reporter(meta.Model):
|
2005-08-25 22:51:30 +00:00
|
|
|
full_name = meta.CharField(maxlength=70)
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return self.full_name
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
class Article(meta.Model):
|
2005-09-02 18:14:44 +00:00
|
|
|
pub_date = meta.DateTimeField)
|
|
|
|
headline = meta.CharField(maxlength=200)
|
|
|
|
article = meta.TextField()
|
2005-08-25 22:51:30 +00:00
|
|
|
reporter = meta.ForeignKey(Reporter)
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return self.headline
|
|
|
|
|
|
|
|
Install it
|
|
|
|
==========
|
|
|
|
|
|
|
|
Next, run the Django command-line utility. It'll create the database tables for
|
|
|
|
you automatically, in the database specified in your Django settings. Django
|
2005-07-17 15:53:56 +00:00
|
|
|
works best with PostgreSQL, although we've recently added beta MySQL
|
|
|
|
support and other database adapters are on the way::
|
2005-07-15 15:31:20 +00:00
|
|
|
|
|
|
|
django-admin.py install news
|
|
|
|
|
|
|
|
Enjoy the free API
|
|
|
|
==================
|
|
|
|
|
|
|
|
With that, you've got a free, and rich, Python API to access your data. The API
|
|
|
|
is created on the fly: No code generation necessary::
|
|
|
|
|
|
|
|
# Modules are dynamically created within django.models.
|
|
|
|
# Their names are plural versions of the model class names.
|
|
|
|
>>> from django.models.news import reporters, articles
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
# No reporters are in the system yet.
|
|
|
|
>>> reporters.get_list()
|
|
|
|
[]
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
# Create a new Reporter.
|
2005-08-01 16:26:39 +00:00
|
|
|
>>> r = reporters.Reporter(full_name='John Smith')
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
# Save the object into the database. You have to call save() explicitly.
|
|
|
|
>>> r.save()
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
# Now it has an ID.
|
|
|
|
>>> r.id
|
|
|
|
1
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
# Now the new reporter is in the database.
|
|
|
|
>>> reporters.get_list()
|
|
|
|
[John Smith]
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
# Fields are represented as attributes on the Python object.
|
|
|
|
>>> r.full_name
|
|
|
|
'John Smith'
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
# Django provides a rich database lookup API that's entirely driven by keyword arguments.
|
|
|
|
>>> reporters.get_object(id__exact=1)
|
|
|
|
John Smith
|
|
|
|
>>> reporters.get_object(full_name__startswith='John')
|
|
|
|
John Smith
|
|
|
|
>>> reporters.get_object(full_name__contains='mith')
|
|
|
|
John Smith
|
|
|
|
>>> reporters.get_object(id__exact=2)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2005-07-16 02:40:24 +00:00
|
|
|
django.models.news.ReporterDoesNotExist: Reporter does not exist for {'id__exact': 2}
|
|
|
|
|
2005-07-26 16:11:43 +00:00
|
|
|
# Lookup by a primary key is the most common case, so Django provides a
|
|
|
|
# shortcut for primary-key exact lookups.
|
|
|
|
# The following is identical to reporters.get_object(id__exact=1).
|
|
|
|
>>> reporters.get_object(pk=1)
|
|
|
|
John Smith
|
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
# Create an article.
|
|
|
|
>>> from datetime import datetime
|
2005-08-01 16:26:39 +00:00
|
|
|
>>> a = articles.Article(pub_date=datetime.now(), headline='Django is cool', article='Yeah.', reporter_id=1)
|
2005-07-15 15:31:20 +00:00
|
|
|
>>> a.save()
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
# Now the article is in the database.
|
|
|
|
>>> articles.get_list()
|
|
|
|
[Django is cool]
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
# Article objects get API access to related Reporter objects.
|
|
|
|
>>> r = a.get_reporter()
|
|
|
|
>>> r.full_name
|
|
|
|
'John Smith'
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
# And vice versa: Reporter objects get API access to Article objects.
|
|
|
|
>>> r.get_article_list()
|
|
|
|
[Django is cool]
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
# The API follows relationships as far as you need.
|
|
|
|
# Find all articles by a reporter whose name starts with "John".
|
|
|
|
>>> articles.get_list(reporter__full_name__startswith="John")
|
|
|
|
[Django is cool]
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
# Change an object by altering its attributes and calling save().
|
|
|
|
>>> r.full_name = 'Billy Goat'
|
|
|
|
>>> r.save()
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
# Delete an object with delete().
|
|
|
|
>>> r.delete()
|
|
|
|
|
|
|
|
A dynamic admin interface: It's not just scaffolding -- it's the whole house
|
|
|
|
============================================================================
|
|
|
|
|
|
|
|
Once your models are defined, Django can automatically create an administrative
|
2005-07-15 15:57:46 +00:00
|
|
|
interface -- a Web site that lets authenticated users add, change and
|
2005-08-25 22:51:30 +00:00
|
|
|
delete objects. It's as easy as adding an extra ``admin`` attribute to your
|
|
|
|
model classes::
|
2005-07-15 15:31:20 +00:00
|
|
|
|
|
|
|
class Article(meta.Model):
|
2005-09-02 18:14:44 +00:00
|
|
|
pub_date = meta.DateTimeField()
|
|
|
|
headline = meta.CharField(maxlength=200)
|
|
|
|
article = meta.TextField()
|
2005-08-25 22:51:30 +00:00
|
|
|
reporter = meta.ForeignKey(Reporter)
|
|
|
|
class META:
|
|
|
|
admin = meta.Admin()
|
2005-07-15 15:31:20 +00:00
|
|
|
|
|
|
|
The philosophy here is that your site is edited by a staff, or a client, or
|
|
|
|
maybe just you -- and you don't want to have to deal with creating backend
|
|
|
|
interfaces just to manage content.
|
|
|
|
|
|
|
|
Our typical workflow at World Online is to create models and get the admin sites
|
|
|
|
up and running as fast as possible, so our staff journalists can start
|
|
|
|
populating data. Then we develop the way data is presented to the public.
|
|
|
|
|
|
|
|
Design your URLs
|
2005-07-15 20:53:17 +00:00
|
|
|
================
|
2005-07-15 15:31:20 +00:00
|
|
|
|
|
|
|
A clean, elegant URL scheme is an important detail in a high-quality Web
|
|
|
|
application. Django lets you design URLs however you want, with no framework
|
|
|
|
limitations.
|
|
|
|
|
|
|
|
To design URLs for an app, you create a Python module. For the above
|
|
|
|
Reporter/Article example, here's what that might look like::
|
|
|
|
|
|
|
|
from django.conf.urls.defaults import *
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
urlpatterns = patterns('',
|
2005-07-19 03:06:28 +00:00
|
|
|
(r'^/articles/(?P<year>\d{4})/$', 'myproject.news.views.articles.year_archive'),
|
|
|
|
(r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'myproject.news.views.articles.month_archive'),
|
2005-08-01 21:37:16 +00:00
|
|
|
(r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<article_id>\d+)/$', 'myproject.news.views.articles.article_detail'),
|
2005-07-15 15:31:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
The code above maps URLs, as regular expressions, to the location of Python
|
|
|
|
callback functions (views). The regular expressions use parenthesis to "capture"
|
|
|
|
values from the URLs. When a user requests a page, Django runs through each
|
|
|
|
regular expression, in order, and stops at the first one that matches the
|
|
|
|
requested URL. (If none of them matches, Django calls a special 404 view.) This
|
|
|
|
is blazingly fast, because the regular expressions are compiled at load time.
|
|
|
|
|
|
|
|
Once one of the regexes matches, Django imports and calls the given view, which
|
2005-07-15 15:57:46 +00:00
|
|
|
is a simple Python function. Each view gets passed a request object --
|
2005-07-15 15:31:20 +00:00
|
|
|
which contains request metadata and lets you access GET and POST data as simple
|
2005-07-15 15:57:46 +00:00
|
|
|
dictionaries -- and the values captured in the regex, via keyword
|
2005-07-15 15:31:20 +00:00
|
|
|
arguments.
|
|
|
|
|
|
|
|
For example, if a user requested the URL "/articles/2005/05/39323/", Django
|
|
|
|
would call the function ``myproject.news.views.articles.article_detail(request,
|
|
|
|
year='2005', month='05', article_id='39323')``.
|
|
|
|
|
|
|
|
Write your views
|
|
|
|
================
|
|
|
|
|
|
|
|
Each view is responsible for doing one of two things: Returning an
|
|
|
|
``HttpResponse`` object containing the content for the requested page, or
|
|
|
|
raising an exception such as ``Http404``. The rest is up to you.
|
|
|
|
|
|
|
|
Generally, a view retrieves data according to the parameters, loads a template
|
|
|
|
and renders the template with the retrieved data. Here's an example view for
|
|
|
|
article_detail from above::
|
|
|
|
|
|
|
|
from django.models.news import articles
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
def article_detail(request, year, month, article_id):
|
|
|
|
# Use the Django API to find an object matching the URL criteria.
|
|
|
|
try:
|
2005-07-26 16:11:43 +00:00
|
|
|
a = articles.get_object(pub_date__year=year, pub_date__month=month, pk=article_id)
|
2005-07-15 15:31:20 +00:00
|
|
|
except articles.ArticleDoesNotExist:
|
|
|
|
raise Http404
|
|
|
|
t = template_loader.get_template('news/article_detail')
|
|
|
|
c = Context(request, {
|
|
|
|
'article': a,
|
|
|
|
})
|
|
|
|
content = t.render(c)
|
|
|
|
return HttpResponse(content)
|
|
|
|
|
|
|
|
This example uses Django's template system, which has several key features.
|
|
|
|
|
|
|
|
Design your templates
|
|
|
|
=====================
|
|
|
|
|
|
|
|
The code above loads the ``news/article_detail`` template.
|
|
|
|
|
|
|
|
Django has a template search path, which allows you to minimize redundancy among
|
|
|
|
templates. In your Django settings, you specify a list of directories to check
|
|
|
|
for templates. If a template doesn't exist in the first directory, it checks the
|
|
|
|
second, and so on.
|
|
|
|
|
|
|
|
Let's say the ``news/article_detail`` template was found. Here's what that might
|
|
|
|
look like::
|
|
|
|
|
|
|
|
{% extends "base" %}
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
{% block title %}{{ article.headline }}{% endblock %}
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
{% block content %}
|
|
|
|
<h1>{{ article.headline }}</h1>
|
|
|
|
<p>By {{ article.get_reporter.full_name }}</p>
|
|
|
|
<p>Published {{ article.pub_date|date:"F j, Y" }}</p>
|
|
|
|
{{ article.article }}
|
|
|
|
{% endblock %}
|
|
|
|
|
|
|
|
|
|
|
|
It should look straightforward. Variables are surrounded by double-curly braces.
|
|
|
|
``{{ article.headline }}`` means "Output the value of the article's headline
|
|
|
|
attribute." But dots aren't used only for attribute lookup: They also can do
|
|
|
|
dictionary-key lookup, index lookup and function calls (as is the case with
|
|
|
|
``article.get_reporter``).
|
|
|
|
|
|
|
|
Note ``{{ article.pub_date|date:"F j, Y" }}`` uses a Unix-style "pipe" (the "|"
|
|
|
|
character). This is called a template filter, and it's a way to filter the value
|
|
|
|
of a variable. In this case, the date filter formats a Python datetime object in
|
|
|
|
the given format (as found in PHP's date function; yes, there is one good idea
|
|
|
|
in PHP).
|
|
|
|
|
|
|
|
You can chain together as many filters as you'd like. You can write custom
|
|
|
|
filters. You can write custom template tags, which run custom Python code behind
|
|
|
|
the scenes.
|
|
|
|
|
|
|
|
Finally, Django uses the concept of template inheritance: That's what the ``{%
|
|
|
|
extends "base" %}`` does. It means "First load the template called 'base', which
|
|
|
|
has defined a bunch of blocks, and fill the blocks with the following blocks."
|
|
|
|
In short, that lets you dramatically cut down on redundancy in templates: Each
|
|
|
|
template has to define only what's unique to that template.
|
|
|
|
|
|
|
|
Here's what the "base" template might look like::
|
|
|
|
|
2005-07-16 02:40:24 +00:00
|
|
|
|
2005-07-15 15:31:20 +00:00
|
|
|
<html>
|
|
|
|
<head>
|
2005-07-16 21:17:58 +00:00
|
|
|
<title>{% block title %}{% endblock %}</title>
|
2005-07-15 15:31:20 +00:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<img src="sitelogo.gif" alt="Logo" />
|
|
|
|
{% block content %}{% endblock %}
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
|
|
|
|
Simplistically, it defines the look-and-feel of the site (with the site's logo),
|
|
|
|
and provides "holes" for child templates to fill. This makes a site redesign as
|
2005-07-15 15:57:46 +00:00
|
|
|
easy as changing a single file -- the base template.
|
2005-07-15 15:31:20 +00:00
|
|
|
|
|
|
|
Note that you don't have to use Django's template system if you prefer another
|
|
|
|
system. While Django's template system is particularly well-integrated with
|
|
|
|
Django's model layer, nothing forces you to use it. For that matter, you don't
|
|
|
|
have to use Django's API, either. You can use another database abstraction
|
|
|
|
layer, you can read XML files, you can read files off disk, or anything you
|
2005-07-15 15:57:46 +00:00
|
|
|
want. Each piece of Django -- models, views, templates -- is decoupled
|
2005-07-15 15:31:20 +00:00
|
|
|
from the next.
|
|
|
|
|
|
|
|
This is just the surface
|
|
|
|
========================
|
|
|
|
|
|
|
|
This has been only a quick overview of Django's functionality. Some more useful
|
|
|
|
features:
|
|
|
|
|
|
|
|
* A caching framework that integrates with memcached or other backends.
|
2005-07-16 02:40:24 +00:00
|
|
|
* An RSS framework that makes creating RSS feeds as easy as writing a
|
2005-07-15 15:31:20 +00:00
|
|
|
small Python class.
|
2005-07-15 15:57:46 +00:00
|
|
|
* More sexy automatically-generated admin features -- this overview barely
|
2005-07-19 17:20:37 +00:00
|
|
|
scratched the surface.
|
2005-07-15 15:31:20 +00:00
|
|
|
|
2005-07-19 17:20:37 +00:00
|
|
|
The next obvious steps are for you to `download Django`_, read `the tutorial`_
|
|
|
|
and join `the community`_. Thanks for your interest!
|
|
|
|
|
|
|
|
.. _download Django: http://www.djangoproject.com/documentation/
|
|
|
|
.. _the tutorial: http://www.djangoproject.com/documentation/tutorial1/
|
|
|
|
.. _the community: http://www.djangoproject.com/community/
|