| 1 |
================= |
|---|
| 2 |
The flatpages app |
|---|
| 3 |
================= |
|---|
| 4 |
|
|---|
| 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. |
|---|
| 8 |
|
|---|
| 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. |
|---|
| 13 |
|
|---|
| 14 |
A flatpage can use a custom template or a default, systemwide flatpage |
|---|
| 15 |
template. It can be associated with one, or multiple, sites. |
|---|
| 16 |
|
|---|
| 17 |
Here are some examples of flatpages on Django-powered sites: |
|---|
| 18 |
|
|---|
| 19 |
* http://www.chicagocrime.org/about/ |
|---|
| 20 |
* http://www.lawrence.com/about/contact/ |
|---|
| 21 |
|
|---|
| 22 |
Installation |
|---|
| 23 |
============ |
|---|
| 24 |
|
|---|
| 25 |
To install the flatpages app, follow these steps: |
|---|
| 26 |
|
|---|
| 27 |
1. Install the `sites framework`_ by adding ``'django.contrib.sites'`` to |
|---|
| 28 |
your INSTALLED_APPS_ setting, if it's not already in there. |
|---|
| 29 |
2. Add ``'django.contrib.flatpages'`` to your INSTALLED_APPS_ setting. |
|---|
| 30 |
3. Add ``'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'`` |
|---|
| 31 |
to your MIDDLEWARE_CLASSES_ setting. |
|---|
| 32 |
4. Run the command ``manage.py syncdb``. |
|---|
| 33 |
|
|---|
| 34 |
.. _sites framework: ../sites/ |
|---|
| 35 |
.. _INSTALLED_APPS: ../settings/#installed-apps |
|---|
| 36 |
.. _MIDDLEWARE_CLASSES: ../settings/#middleware-classes |
|---|
| 37 |
|
|---|
| 38 |
How it works |
|---|
| 39 |
============ |
|---|
| 40 |
|
|---|
| 41 |
``manage.py syncdb`` creates two tables in your database: ``django_flatpage`` |
|---|
| 42 |
and ``django_flatpage_sites``. ``django_flatpage`` is a simple lookup table |
|---|
| 43 |
that simply maps a URL to a title and bunch of text content. |
|---|
| 44 |
``django_flatpage_sites`` associates a flatpage with a site. |
|---|
| 45 |
|
|---|
| 46 |
The ``FlatpageFallbackMiddleware`` does all of the work. Each time any Django |
|---|
| 47 |
application raises a 404 error, this middleware checks the flatpages database |
|---|
| 48 |
for the requested URL as a last resort. Specifically, it checks for a flatpage |
|---|
| 49 |
with the given URL with a site ID that corresponds to the SITE_ID_ setting. |
|---|
| 50 |
|
|---|
| 51 |
If it finds a match, it follows this algorithm: |
|---|
| 52 |
|
|---|
| 53 |
* If the flatpage has a custom template, it loads that template. Otherwise, |
|---|
| 54 |
it loads the template ``flatpages/default.html``. |
|---|
| 55 |
* It passes that template a single context variable, ``flatpage``, which is |
|---|
| 56 |
the flatpage object. It uses RequestContext_ in rendering the template. |
|---|
| 57 |
|
|---|
| 58 |
If it doesn't find a match, the request continues to be processed as usual. |
|---|
| 59 |
|
|---|
| 60 |
The middleware only gets activated for 404s -- not for 500s or responses of any |
|---|
| 61 |
other status code. |
|---|
| 62 |
|
|---|
| 63 |
Note that the order of ``MIDDLEWARE_CLASSES`` matters. Generally, you can put |
|---|
| 64 |
``FlatpageFallbackMiddleware`` at the end of the list, because it's a last |
|---|
| 65 |
resort. |
|---|
| 66 |
|
|---|
| 67 |
For more on middleware, read the `middleware docs`_. |
|---|
| 68 |
|
|---|
| 69 |
.. _SITE_ID: ../settings/#site-id |
|---|
| 70 |
.. _RequestContext: ../templates_python/#subclassing-context-djangocontext |
|---|
| 71 |
.. _middleware docs: ../middleware/ |
|---|
| 72 |
|
|---|
| 73 |
How to add, change and delete flatpages |
|---|
| 74 |
======================================= |
|---|
| 75 |
|
|---|
| 76 |
Via the admin interface |
|---|
| 77 |
----------------------- |
|---|
| 78 |
|
|---|
| 79 |
If you've activated the automatic Django admin interface, you should see a |
|---|
| 80 |
"Flatpages" section on the admin index page. Edit flatpages as you edit any |
|---|
| 81 |
other object in the system. |
|---|
| 82 |
|
|---|
| 83 |
Via the Python API |
|---|
| 84 |
------------------ |
|---|
| 85 |
|
|---|
| 86 |
Flatpages are represented by a standard `Django model`_, which lives in |
|---|
| 87 |
`django/contrib/flatpages/models.py`_. You can access flatpage objects via the |
|---|
| 88 |
`Django database API`_. |
|---|
| 89 |
|
|---|
| 90 |
.. _Django model: ../model-api/ |
|---|
| 91 |
.. _django/contrib/flatpages/models.py: http://code.djangoproject.com/browser/django/trunk/django/contrib/flatpages/models.py |
|---|
| 92 |
.. _Django database API: ../db-api/ |
|---|
| 93 |
|
|---|
| 94 |
Flatpage templates |
|---|
| 95 |
================== |
|---|
| 96 |
|
|---|
| 97 |
By default, flatpages are rendered via the template ``flatpages/default.html``, |
|---|
| 98 |
but you can override that for a particular flatpage. |
|---|
| 99 |
|
|---|
| 100 |
Creating the ``flatpages/default.html`` template is your responsibility; in |
|---|
| 101 |
your template directory, just create a ``flatpages`` directory containing a |
|---|
| 102 |
file ``default.html``. |
|---|
| 103 |
|
|---|
| 104 |
Flatpage templates are passed a single context variable, ``flatpage``, which is |
|---|
| 105 |
the flatpage object. |
|---|
| 106 |
|
|---|
| 107 |
Here's a sample ``flatpages/default.html`` template:: |
|---|
| 108 |
|
|---|
| 109 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" |
|---|
| 110 |
"http://www.w3.org/TR/REC-html40/loose.dtd"> |
|---|
| 111 |
<html> |
|---|
| 112 |
<head> |
|---|
| 113 |
<title>{{ flatpage.title }}</title> |
|---|
| 114 |
</head> |
|---|
| 115 |
<body> |
|---|
| 116 |
{{ flatpage.content }} |
|---|
| 117 |
</body> |
|---|
| 118 |
</html> |
|---|
| 119 |
|
|---|
| 120 |
Since you're already entering raw HTML into the admin page for a flatpage, |
|---|
| 121 |
both ``flatpage.title`` and ``flatpage.content`` are marked as **not** |
|---|
| 122 |
requiring `automatic HTML escaping`_ in the template. |
|---|
| 123 |
|
|---|
| 124 |
.. _automatic HTML escaping: ../templates/#automatic-html-escaping |
|---|