2005-07-15 23:57:46 +08:00
|
|
|
==================
|
|
|
|
Django at a glance
|
|
|
|
==================
|
2005-07-15 23:31:20 +08: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.
|
|
|
|
|
2007-01-25 04:08:47 +08:00
|
|
|
.. _documentation: ../
|
2005-07-15 23:31:20 +08:00
|
|
|
|
|
|
|
Design your model
|
|
|
|
=================
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Although you can use Django without a database, it comes with an
|
|
|
|
object-relational mapper in which you describe your database layout in Python
|
|
|
|
code.
|
2005-07-15 23:31:20 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
The data-model syntax offers many rich ways of representing your models -- so
|
|
|
|
far, it's been solving two years' worth of database-schema problems. Here's a
|
|
|
|
quick example::
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Reporter(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
full_name = models.CharField(max_length=70)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2005-07-15 23:31:20 +08:00
|
|
|
return self.full_name
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Article(models.Model):
|
|
|
|
pub_date = models.DateTimeField()
|
2007-08-05 13:14:46 +08:00
|
|
|
headline = models.CharField(max_length=200)
|
2008-07-30 18:08:12 +08:00
|
|
|
content = models.TextField()
|
2006-05-02 09:31:56 +08:00
|
|
|
reporter = models.ForeignKey(Reporter)
|
2005-07-16 10:40:24 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2005-07-15 23:31:20 +08:00
|
|
|
return self.headline
|
|
|
|
|
|
|
|
Install it
|
|
|
|
==========
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Next, run the Django command-line utility to create the database tables
|
|
|
|
automatically::
|
|
|
|
|
|
|
|
manage.py syncdb
|
2005-07-15 23:31:20 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
The ``syncdb`` command looks at all your available models and creates tables
|
|
|
|
in your database for whichever tables don't already exist.
|
2005-07-15 23:31:20 +08:00
|
|
|
|
|
|
|
Enjoy the free API
|
|
|
|
==================
|
|
|
|
|
|
|
|
With that, you've got a free, and rich, Python API to access your data. The API
|
2007-08-07 10:33:11 +08:00
|
|
|
is created on the fly, no code generation necessary::
|
2005-07-15 23:31:20 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> from mysite.models import Reporter, Article
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2005-07-15 23:31:20 +08:00
|
|
|
# No reporters are in the system yet.
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Reporter.objects.all()
|
2005-07-15 23:31:20 +08:00
|
|
|
[]
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2005-07-15 23:31:20 +08:00
|
|
|
# Create a new Reporter.
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> r = Reporter(full_name='John Smith')
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2005-07-15 23:31:20 +08:00
|
|
|
# Save the object into the database. You have to call save() explicitly.
|
|
|
|
>>> r.save()
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2005-07-15 23:31:20 +08:00
|
|
|
# Now it has an ID.
|
|
|
|
>>> r.id
|
|
|
|
1
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2005-07-15 23:31:20 +08:00
|
|
|
# Now the new reporter is in the database.
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Reporter.objects.all()
|
2005-07-15 23:31:20 +08:00
|
|
|
[John Smith]
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2005-07-15 23:31:20 +08:00
|
|
|
# Fields are represented as attributes on the Python object.
|
|
|
|
>>> r.full_name
|
|
|
|
'John Smith'
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
# Django provides a rich database lookup API.
|
|
|
|
>>> Reporter.objects.get(id=1)
|
2005-07-15 23:31:20 +08:00
|
|
|
John Smith
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Reporter.objects.get(full_name__startswith='John')
|
2005-07-15 23:31:20 +08:00
|
|
|
John Smith
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Reporter.objects.get(full_name__contains='mith')
|
2005-07-15 23:31:20 +08:00
|
|
|
John Smith
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Reporter.objects.get(id=2)
|
2005-07-15 23:31:20 +08:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2006-05-02 09:31:56 +08:00
|
|
|
DoesNotExist: Reporter does not exist for {'id__exact': 2}
|
2005-07-27 00:11:43 +08:00
|
|
|
|
2005-07-15 23:31:20 +08:00
|
|
|
# Create an article.
|
|
|
|
>>> from datetime import datetime
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> a = Article(pub_date=datetime.now(), headline='Django is cool',
|
2008-07-30 18:08:12 +08:00
|
|
|
... content='Yeah.', reporter=r)
|
2005-07-15 23:31:20 +08:00
|
|
|
>>> a.save()
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2005-07-15 23:31:20 +08:00
|
|
|
# Now the article is in the database.
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Article.objects.all()
|
2005-07-15 23:31:20 +08:00
|
|
|
[Django is cool]
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2005-07-15 23:31:20 +08:00
|
|
|
# Article objects get API access to related Reporter objects.
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> r = a.reporter
|
2005-07-15 23:31:20 +08:00
|
|
|
>>> r.full_name
|
|
|
|
'John Smith'
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2005-07-15 23:31:20 +08:00
|
|
|
# And vice versa: Reporter objects get API access to Article objects.
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> r.article_set.all()
|
2005-07-15 23:31:20 +08:00
|
|
|
[Django is cool]
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
# The API follows relationships as far as you need, performing efficient
|
|
|
|
# JOINs for you behind the scenes.
|
|
|
|
# This finds all articles by a reporter whose name starts with "John".
|
|
|
|
>>> Article.objects.filter(reporter__full_name__startswith="John")
|
2005-07-15 23:31:20 +08:00
|
|
|
[Django is cool]
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2005-07-15 23:31:20 +08:00
|
|
|
# Change an object by altering its attributes and calling save().
|
|
|
|
>>> r.full_name = 'Billy Goat'
|
|
|
|
>>> r.save()
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2005-07-15 23:31:20 +08:00
|
|
|
# Delete an object with delete().
|
|
|
|
>>> r.delete()
|
|
|
|
|
2007-08-07 10:33:11 +08:00
|
|
|
A dynamic admin interface: it's not just scaffolding -- it's the whole house
|
2005-07-15 23:31:20 +08:00
|
|
|
============================================================================
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Once your models are defined, Django can automatically create a professional,
|
|
|
|
production ready administrative interface -- a Web site that lets authenticated
|
|
|
|
users add, change and delete objects. It's as easy as adding a line of code to
|
|
|
|
your model classes::
|
2005-07-15 23:31:20 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Article(models.Model):
|
|
|
|
pub_date = models.DateTimeField()
|
2007-08-05 13:14:46 +08:00
|
|
|
headline = models.CharField(max_length=200)
|
2008-07-30 18:08:12 +08:00
|
|
|
content = models.TextField()
|
2006-05-02 09:31:56 +08:00
|
|
|
reporter = models.ForeignKey(Reporter)
|
|
|
|
class Admin: pass
|
2005-07-15 23:31:20 +08: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.
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
One typical workflow in creating Django apps is to create models and get the
|
|
|
|
admin sites up and running as fast as possible, so your staff (or clients) can
|
|
|
|
start populating data. Then, develop the way data is presented to the public.
|
2005-07-15 23:31:20 +08:00
|
|
|
|
|
|
|
Design your URLs
|
2005-07-16 04:53:17 +08:00
|
|
|
================
|
2005-07-15 23:31:20 +08:00
|
|
|
|
|
|
|
A clean, elegant URL scheme is an important detail in a high-quality Web
|
2006-05-02 09:31:56 +08:00
|
|
|
application. Django encourages beautiful URL design and doesn't put any cruft
|
|
|
|
in URLs, like ``.php`` or ``.asp``.
|
2005-07-15 23:31:20 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
To design URLs for an app, you create a Python module called a URLconf. A table
|
|
|
|
of contents for your app, it contains a simple mapping between URL patterns and
|
|
|
|
Python callback functions. URLconfs also serve to decouple URLs from Python
|
|
|
|
code.
|
|
|
|
|
2006-08-24 11:37:34 +08:00
|
|
|
Here's what a URLconf might look like for the ``Reporter``/``Article``
|
2006-05-02 09:31:56 +08:00
|
|
|
example above::
|
2005-07-15 23:31:20 +08:00
|
|
|
|
|
|
|
from django.conf.urls.defaults import *
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2005-07-15 23:31:20 +08:00
|
|
|
urlpatterns = patterns('',
|
2007-07-10 10:34:42 +08:00
|
|
|
(r'^articles/(\d{4})/$', 'mysite.views.year_archive'),
|
|
|
|
(r'^articles/(\d{4})/(\d{2})/$', 'mysite.views.month_archive'),
|
|
|
|
(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'mysite.views.article_detail'),
|
2005-07-15 23:31:20 +08:00
|
|
|
)
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
The code above maps URLs, as simple 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 pattern, in order, and stops at the first one that matches the
|
|
|
|
requested URL. (If none of them matches, Django calls a special-case 404 view.)
|
|
|
|
This is blazingly fast, because the regular expressions are compiled at load
|
|
|
|
time.
|
2005-07-15 23:31:20 +08:00
|
|
|
|
|
|
|
Once one of the regexes matches, Django imports and calls the given view, which
|
2005-07-15 23:57:46 +08:00
|
|
|
is a simple Python function. Each view gets passed a request object --
|
2006-05-02 09:31:56 +08:00
|
|
|
which contains request metadata -- and the values captured in the regex.
|
2005-07-15 23:31:20 +08:00
|
|
|
|
|
|
|
For example, if a user requested the URL "/articles/2005/05/39323/", Django
|
2006-05-09 23:31:03 +08:00
|
|
|
would call the function ``mysite.views.article_detail(request,
|
2006-05-02 09:31:56 +08:00
|
|
|
'2005', '05', '39323')``.
|
2005-07-15 23:31:20 +08:00
|
|
|
|
|
|
|
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
|
2006-05-02 09:31:56 +08:00
|
|
|
``year_archive`` from above::
|
2005-07-15 23:31:20 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def year_archive(request, year):
|
|
|
|
a_list = Article.objects.filter(pub_date__year=year)
|
2006-05-09 05:59:37 +08:00
|
|
|
return render_to_response('news/year_archive.html', {'year': year, 'article_list': a_list})
|
2005-07-15 23:31:20 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
This example uses Django's template system, which has several powerful
|
|
|
|
features but strives to stay simple enough for non-programmers to use.
|
2005-07-15 23:31:20 +08:00
|
|
|
|
|
|
|
Design your templates
|
|
|
|
=====================
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
The code above loads the ``news/year_archive.html`` template.
|
2005-07-15 23:31:20 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Let's say the ``news/article_detail.html`` template was found. Here's what that
|
|
|
|
might look like::
|
2005-07-15 23:31:20 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
{% extends "base.html" %}
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2006-05-09 05:59:37 +08:00
|
|
|
{% block title %}Articles for {{ year }}{% endblock %}
|
2005-07-16 10:40:24 +08:00
|
|
|
|
2005-07-15 23:31:20 +08:00
|
|
|
{% block content %}
|
2006-05-09 05:59:37 +08:00
|
|
|
<h1>Articles for {{ year }}</h1>
|
|
|
|
|
|
|
|
{% for article in article_list %}
|
|
|
|
<p>{{ article.headline }}</p>
|
|
|
|
<p>By {{ article.reporter.full_name }}</p>
|
2005-07-15 23:31:20 +08:00
|
|
|
<p>Published {{ article.pub_date|date:"F j, Y" }}</p>
|
2006-05-09 05:59:37 +08:00
|
|
|
{% endfor %}
|
2005-07-15 23:31:20 +08:00
|
|
|
{% endblock %}
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
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
|
2006-05-09 23:31:03 +08:00
|
|
|
lookup and function calls.
|
2005-07-15 23:31:20 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Finally, Django uses the concept of "template inheritance": That's what the
|
|
|
|
``{% extends "base.html" %}`` 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
|
2007-08-07 10:33:11 +08:00
|
|
|
in templates: each template has to define only what's unique to that template.
|
2005-07-15 23:31:20 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Here's what the "base.html" template might look like::
|
2005-07-15 23:31:20 +08:00
|
|
|
|
|
|
|
<html>
|
|
|
|
<head>
|
2005-07-17 05:17:58 +08:00
|
|
|
<title>{% block title %}{% endblock %}</title>
|
2005-07-15 23:31:20 +08: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 23:57:46 +08:00
|
|
|
easy as changing a single file -- the base template.
|
2005-07-15 23:31:20 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
It also lets you create multiple versions of a site, with different base
|
|
|
|
templates, while reusing child templates. Django's creators have used this
|
|
|
|
technique to create strikingly different cell-phone editions of sites -- simply
|
|
|
|
by creating a new base template.
|
|
|
|
|
2005-07-15 23:31:20 +08: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
|
2006-05-02 09:31:56 +08:00
|
|
|
have to use Django's database API, either. You can use another database
|
|
|
|
abstraction layer, you can read XML files, you can read files off disk, or
|
|
|
|
anything you want. Each piece of Django -- models, views, templates -- is
|
|
|
|
decoupled from the next.
|
2005-07-15 23:31:20 +08:00
|
|
|
|
|
|
|
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.
|
2007-08-04 11:19:14 +08:00
|
|
|
* A `syndication framework`_ that makes creating RSS and Atom feeds as easy as
|
2005-11-20 02:20:30 +08:00
|
|
|
writing a small Python class.
|
2005-07-15 23:57:46 +08:00
|
|
|
* More sexy automatically-generated admin features -- this overview barely
|
2005-07-20 01:20:37 +08:00
|
|
|
scratched the surface.
|
2005-07-15 23:31:20 +08:00
|
|
|
|
2007-08-04 11:19:14 +08:00
|
|
|
.. _syndication framework: ../syndication_feeds/
|
|
|
|
|
2005-07-20 01:20:37 +08:00
|
|
|
The next obvious steps are for you to `download Django`_, read `the tutorial`_
|
|
|
|
and join `the community`_. Thanks for your interest!
|
|
|
|
|
2005-11-14 07:10:27 +08:00
|
|
|
.. _download Django: http://www.djangoproject.com/download/
|
2007-08-04 11:39:24 +08:00
|
|
|
.. _the tutorial: ../tutorial01/
|
2005-07-20 01:20:37 +08:00
|
|
|
.. _the community: http://www.djangoproject.com/community/
|