5
Django comes with an optional "flatpages" application. It lets you store simple
6
"flat" HTML content in a database and handles the management for you via
7
Django's admin interface and a Python API.
9
A flatpage is a simple object with a URL, title and content. Use it for
10
one-off, special-case pages, such as "About" or "Privacy Policy" pages, that
11
you want to store in a database but for which you don't want to develop a
12
custom Django application.
14
A flatpage can use a custom template or a default, systemwide flatpage
15
template. It can be associated with one, or multiple, sites.
17
Here are some examples of flatpages on Django-powered sites:
19
* http://www.chicagocrime.org/about/
20
* http://www.lawrence.com/about/contact/
25
To install the flatpages app, follow these steps:
27
1. Add ``'django.contrib.flatpages'`` to your INSTALLED_APPS_ setting.
28
2. Add ``'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'``
29
to your MIDDLEWARE_CLASSES_ setting.
30
3. Run the command ``manage.py syncdb``.
32
.. _INSTALLED_APPS: ../settings/#installed-apps
33
.. _MIDDLEWARE_CLASSES: ../settings/#middleware-classes
38
``manage.py syncdb`` creates two tables in your database: ``django_flatpage``
39
and ``django_flatpage_sites``. ``django_flatpage`` is a simple lookup table
40
that simply maps a URL to a title and bunch of text content.
41
``django_flatpage_sites`` associates a flatpage with a site.
43
The ``FlatpageFallbackMiddleware`` does all of the work. Each time any Django
44
application raises a 404 error, this middleware checks the flatpages database
45
for the requested URL as a last resort. Specifically, it checks for a flatpage
46
with the given URL with a site ID that corresponds to the SITE_ID_ setting.
48
If it finds a match, it follows this algorithm:
50
* If the flatpage has a custom template, it loads that template. Otherwise,
51
it loads the template ``flatpages/default``.
52
* It passes that template a single context variable, ``flatpage``, which is
53
the flatpage object. It uses RequestContext_ in rendering the template.
55
If it doesn't find a match, the request continues to be processed as usual.
57
The middleware only gets activated for 404s -- not for 500s or responses of any
60
Note that the order of ``MIDDLEWARE_CLASSES`` matters. Generally, you can put
61
``FlatpageFallbackMiddleware`` at the end of the list, because it's a last
64
For more on middleware, read the `middleware docs`_.
66
.. _SITE_ID: ../settings/#site-id
67
.. _RequestContext: ../templates_python/#subclassing-context-djangocontext
68
.. _middleware docs: ../middleware/
70
How to add, change and delete flatpages
71
=======================================
73
Via the admin interface
74
-----------------------
76
If you've activated the automatic Django admin interface, you should see a
77
"Flatpages" section on the admin index page. Edit flatpages as you edit any
78
other object in the system.
83
Flatpages are represented by a standard `Django model`_, which lives in
84
`django/contrib/flatpages/models.py`_. You can access flatpage objects via the
85
`Django database API`_.
87
.. _Django model: ../model_api/
88
.. _django/contrib/flatpages/models.py: http://code.djangoproject.com/browser/django/trunk/django/contrib/flatpages/models.py
89
.. _Django database API: ../db_api/
94
By default, flatpages are rendered via the template ``flatpages/default.html``,
95
but you can override that for a particular flatpage.
97
Creating the ``flatpages/default.html`` template is your responsibility; in
98
your template directory, just create a ``flatpages`` directory containing a
99
file ``default.html``.
101
Flatpage templates are passed a single context variable, ``flatpage``, which is
104
Here's a sample ``flatpages/default.html`` template::
106
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
107
"http://www.w3.org/TR/REC-html40/loose.dtd">
110
<title>{{ flatpage.title }}</title>
113
{{ flatpage.content }}