2005-11-11 05:45:05 +01:00
|
|
|
=================
|
|
|
|
The flatpages app
|
|
|
|
=================
|
|
|
|
|
|
|
|
Django comes with an optional "flatpages" application. It lets you store simple
|
2006-05-02 03:31:56 +02:00
|
|
|
"flat" HTML content in a database and handles the management for you via
|
|
|
|
Django's admin interface and a Python API.
|
2005-11-11 05:45:05 +01:00
|
|
|
|
|
|
|
A flatpage is a simple object with a URL, title and content. Use it for
|
|
|
|
one-off, special-case pages, such as "About" or "Privacy Policy" pages, that
|
|
|
|
you want to store in a database but for which you don't want to develop a
|
|
|
|
custom Django application.
|
|
|
|
|
|
|
|
A flatpage can use a custom template or a default, systemwide flatpage
|
|
|
|
template. It can be associated with one, or multiple, sites.
|
|
|
|
|
|
|
|
Here are some examples of flatpages on Django-powered sites:
|
|
|
|
|
|
|
|
* http://www.chicagocrime.org/about/
|
|
|
|
* http://www.lawrence.com/about/contact/
|
|
|
|
|
|
|
|
Installation
|
|
|
|
============
|
|
|
|
|
2006-03-16 03:57:30 +01:00
|
|
|
To install the flatpages app, follow these steps:
|
2005-11-11 05:45:05 +01:00
|
|
|
|
2006-05-02 03:31:56 +02:00
|
|
|
1. Add ``'django.contrib.flatpages'`` to your INSTALLED_APPS_ setting.
|
|
|
|
2. Add ``'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'``
|
2005-11-11 05:45:05 +01:00
|
|
|
to your MIDDLEWARE_CLASSES_ setting.
|
2006-05-02 03:31:56 +02:00
|
|
|
3. Run the command ``manage.py syncdb``.
|
2005-11-11 05:45:05 +01:00
|
|
|
|
2007-01-24 21:08:47 +01:00
|
|
|
.. _INSTALLED_APPS: ../settings/#installed-apps
|
|
|
|
.. _MIDDLEWARE_CLASSES: ../settings/#middleware-classes
|
2005-11-11 05:45:05 +01:00
|
|
|
|
|
|
|
How it works
|
|
|
|
============
|
|
|
|
|
2006-05-02 03:31:56 +02:00
|
|
|
``manage.py syncdb`` creates two tables in your database: ``django_flatpage``
|
|
|
|
and ``django_flatpage_sites``. ``django_flatpage`` is a simple lookup table
|
|
|
|
that simply maps a URL to a title and bunch of text content.
|
|
|
|
``django_flatpage_sites`` associates a flatpage with a site.
|
2005-11-11 05:45:05 +01:00
|
|
|
|
|
|
|
The ``FlatpageFallbackMiddleware`` does all of the work. Each time any Django
|
|
|
|
application raises a 404 error, this middleware checks the flatpages database
|
|
|
|
for the requested URL as a last resort. Specifically, it checks for a flatpage
|
|
|
|
with the given URL with a site ID that corresponds to the SITE_ID_ setting.
|
|
|
|
|
|
|
|
If it finds a match, it follows this algorithm:
|
|
|
|
|
|
|
|
* If the flatpage has a custom template, it loads that template. Otherwise,
|
|
|
|
it loads the template ``flatpages/default``.
|
|
|
|
* It passes that template a single context variable, ``flatpage``, which is
|
2006-05-02 03:31:56 +02:00
|
|
|
the flatpage object. It uses RequestContext_ in rendering the template.
|
2005-11-11 05:45:05 +01:00
|
|
|
|
|
|
|
If it doesn't find a match, the request continues to be processed as usual.
|
|
|
|
|
|
|
|
The middleware only gets activated for 404s -- not for 500s or responses of any
|
|
|
|
other status code.
|
|
|
|
|
|
|
|
Note that the order of ``MIDDLEWARE_CLASSES`` matters. Generally, you can put
|
|
|
|
``FlatpageFallbackMiddleware`` at the end of the list, because it's a last
|
|
|
|
resort.
|
|
|
|
|
|
|
|
For more on middleware, read the `middleware docs`_.
|
|
|
|
|
2007-01-24 21:08:47 +01:00
|
|
|
.. _SITE_ID: ../settings/#site-id
|
|
|
|
.. _RequestContext: ../templates_python/#subclassing-context-djangocontext
|
|
|
|
.. _middleware docs: ../middleware/
|
2005-11-11 05:45:05 +01:00
|
|
|
|
|
|
|
How to add, change and delete flatpages
|
|
|
|
=======================================
|
|
|
|
|
|
|
|
Via the admin interface
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
If you've activated the automatic Django admin interface, you should see a
|
|
|
|
"Flatpages" section on the admin index page. Edit flatpages as you edit any
|
|
|
|
other object in the system.
|
|
|
|
|
|
|
|
Via the Python API
|
|
|
|
------------------
|
|
|
|
|
|
|
|
Flatpages are represented by a standard `Django model`_, which lives in
|
2006-05-02 03:31:56 +02:00
|
|
|
`django/contrib/flatpages/models.py`_. You can access flatpage objects via the
|
|
|
|
`Django database API`_.
|
2005-11-11 05:45:05 +01:00
|
|
|
|
2007-04-24 07:58:03 +02:00
|
|
|
.. _Django model: ../model-api/
|
2006-05-02 04:01:19 +02:00
|
|
|
.. _django/contrib/flatpages/models.py: http://code.djangoproject.com/browser/django/trunk/django/contrib/flatpages/models.py
|
2007-04-24 07:58:03 +02:00
|
|
|
.. _Django database API: ../db-api/
|
2005-11-11 05:45:05 +01:00
|
|
|
|
|
|
|
Flatpage templates
|
|
|
|
==================
|
|
|
|
|
2006-05-02 03:31:56 +02:00
|
|
|
By default, flatpages are rendered via the template ``flatpages/default.html``,
|
|
|
|
but you can override that for a particular flatpage.
|
2005-11-11 05:45:05 +01:00
|
|
|
|
2006-05-02 03:31:56 +02:00
|
|
|
Creating the ``flatpages/default.html`` template is your responsibility; in
|
|
|
|
your template directory, just create a ``flatpages`` directory containing a
|
|
|
|
file ``default.html``.
|
2005-11-11 05:45:05 +01:00
|
|
|
|
|
|
|
Flatpage templates are passed a single context variable, ``flatpage``, which is
|
|
|
|
the flatpage object.
|
|
|
|
|
2006-05-02 03:31:56 +02:00
|
|
|
Here's a sample ``flatpages/default.html`` template::
|
2005-11-11 05:45:05 +01:00
|
|
|
|
|
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
|
|
|
|
"http://www.w3.org/TR/REC-html40/loose.dtd">
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>{{ flatpage.title }}</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
{{ flatpage.content }}
|
|
|
|
</body>
|
|
|
|
</html>
|