2005-11-11 04:45:05 +00:00
|
|
|
=================
|
|
|
|
The flatpages app
|
|
|
|
=================
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. module:: django.contrib.flatpages
|
|
|
|
:synopsis: A framework for managing simple ?flat? HTML content in a database.
|
|
|
|
|
2005-11-11 04:45:05 +00:00
|
|
|
Django comes with an optional "flatpages" application. It lets you store simple
|
2006-05-02 01:31:56 +00: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 04:45:05 +00: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.
|
|
|
|
|
2010-08-28 11:59:14 +00:00
|
|
|
The content field may optionally be left blank if you prefer to put your
|
2008-06-30 13:05:47 +00:00
|
|
|
content in a custom template.
|
|
|
|
|
2005-11-11 04:45:05 +00:00
|
|
|
Here are some examples of flatpages on Django-powered sites:
|
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* http://www.lawrence.com/about/contact/
|
|
|
|
* http://www2.ljworld.com/site/rules/
|
2005-11-11 04:45:05 +00:00
|
|
|
|
|
|
|
Installation
|
|
|
|
============
|
|
|
|
|
2006-03-16 02:57:30 +00:00
|
|
|
To install the flatpages app, follow these steps:
|
2005-11-11 04:45:05 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
1. Install the :mod:`sites framework <django.contrib.sites>` by adding
|
|
|
|
``'django.contrib.sites'`` to your :setting:`INSTALLED_APPS` setting,
|
|
|
|
if it's not already in there.
|
2010-08-28 11:59:14 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
Also make sure you've correctly set :setting:`SITE_ID` to the ID of the
|
|
|
|
site the settings file represents. This will usually be ``1`` (i.e.
|
|
|
|
``SITE_ID = 1``, but if you're using the sites framework to manage
|
|
|
|
multiple sites, it could be the ID of a different site.
|
2010-08-28 11:59:14 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
2. Add ``'django.contrib.flatpages'`` to your :setting:`INSTALLED_APPS`
|
|
|
|
setting.
|
2010-08-28 11:59:14 +00:00
|
|
|
|
2012-08-31 10:50:00 +00:00
|
|
|
Then either:
|
|
|
|
|
|
|
|
3. Add an entry in your URLconf. For example::
|
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
urlpatterns = [
|
|
|
|
url(r'^pages/', include('django.contrib.flatpages.urls')),
|
|
|
|
]
|
2012-08-31 10:50:00 +00:00
|
|
|
|
|
|
|
or:
|
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
3. Add ``'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'``
|
|
|
|
to your :setting:`MIDDLEWARE_CLASSES` setting.
|
2010-08-28 11:59:14 +00:00
|
|
|
|
2013-07-25 15:19:36 +00:00
|
|
|
4. Run the command :djadmin:`manage.py migrate <migrate>`.
|
2010-08-28 11:59:14 +00:00
|
|
|
|
2011-02-18 00:58:34 +00:00
|
|
|
.. currentmodule:: django.contrib.flatpages.middleware
|
|
|
|
|
2005-11-11 04:45:05 +00:00
|
|
|
How it works
|
|
|
|
============
|
|
|
|
|
2013-07-25 15:19:36 +00:00
|
|
|
``manage.py migrate`` creates two tables in your database: ``django_flatpage``
|
2006-05-02 01:31:56 +00:00
|
|
|
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 04:45:05 +00:00
|
|
|
|
2012-08-31 10:50:00 +00:00
|
|
|
Using the URLconf
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
There are several ways to include the flat pages in your URLconf. You can
|
|
|
|
dedicate a particular path to flat pages::
|
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
urlpatterns = [
|
|
|
|
url(r'^pages/', include('django.contrib.flatpages.urls')),
|
|
|
|
]
|
2012-08-31 10:50:00 +00:00
|
|
|
|
|
|
|
You can also set it up as a "catchall" pattern. In this case, it is important
|
|
|
|
to place the pattern at the end of the other urlpatterns::
|
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
from django.contrib.flatpages import views
|
|
|
|
|
2012-08-31 10:50:00 +00:00
|
|
|
# Your other patterns here
|
2014-04-02 00:46:34 +00:00
|
|
|
urlpatterns += [
|
|
|
|
url(r'^(?P<url>.*/)$', views.flatpage),
|
|
|
|
]
|
2012-08-31 10:50:00 +00:00
|
|
|
|
2013-06-11 16:25:39 +00:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
If you set :setting:`APPEND_SLASH` to ``False``, you must remove the slash
|
|
|
|
in the catchall pattern or flatpages without a trailing slash will not be
|
|
|
|
matched.
|
|
|
|
|
2012-08-31 10:50:00 +00:00
|
|
|
Another common setup is to use flat pages for a limited set of known pages and
|
|
|
|
to hard code the urls, so you can reference them with the :ttag:`url` template
|
|
|
|
tag::
|
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
from django.contrib.flatpages import views
|
|
|
|
|
|
|
|
urlpatterns += [
|
|
|
|
url(r'^about-us/$', views.flatpage, {'url': '/about-us/'}, name='about'),
|
|
|
|
url(r'^license/$', views.flatpage, {'url': '/license/'}, name='license'),
|
|
|
|
]
|
2012-08-31 10:50:00 +00:00
|
|
|
|
|
|
|
Using the middleware
|
|
|
|
--------------------
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
The :class:`~django.contrib.flatpages.middleware.FlatpageFallbackMiddleware`
|
2012-08-31 10:50:00 +00:00
|
|
|
can do all of the work.
|
2011-02-18 00:58:34 +00:00
|
|
|
|
|
|
|
.. class:: FlatpageFallbackMiddleware
|
|
|
|
|
|
|
|
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 :setting:`SITE_ID` setting.
|
2005-11-11 04:45:05 +00:00
|
|
|
|
2011-02-18 00:58:34 +00:00
|
|
|
If it finds a match, it follows this algorithm:
|
2005-11-11 04:45:05 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* If the flatpage has a custom template, it loads that template.
|
|
|
|
Otherwise, it loads the template :file:`flatpages/default.html`.
|
2010-08-28 11:59:14 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* It passes that template a single context variable, ``flatpage``,
|
|
|
|
which is the flatpage object. It uses
|
|
|
|
:class:`~django.template.RequestContext` in rendering the
|
|
|
|
template.
|
2005-11-11 04:45:05 +00:00
|
|
|
|
2012-12-26 20:47:29 +00:00
|
|
|
The middleware will only add a trailing slash and redirect (by looking
|
|
|
|
at the :setting:`APPEND_SLASH` setting) if the resulting URL refers to
|
|
|
|
a valid flatpage. Redirects are permanent (301 status code).
|
2011-04-20 14:41:47 +00:00
|
|
|
|
2011-02-18 00:58:34 +00:00
|
|
|
If it doesn't find a match, the request continues to be processed as usual.
|
2005-11-11 04:45:05 +00:00
|
|
|
|
2011-02-18 00:58:34 +00:00
|
|
|
The middleware only gets activated for 404s -- not for 500s or responses
|
|
|
|
of any other status code.
|
2005-11-11 04:45:05 +00:00
|
|
|
|
2010-03-03 08:51:46 +00:00
|
|
|
.. admonition:: Flatpages will not apply view middleware
|
|
|
|
|
|
|
|
Because the ``FlatpageFallbackMiddleware`` is applied only after
|
|
|
|
URL resolution has failed and produced a 404, the response it
|
2010-03-10 00:58:05 +00:00
|
|
|
returns will not apply any :ref:`view middleware <view-middleware>`
|
2010-03-03 08:51:46 +00:00
|
|
|
methods. Only requests which are successfully routed to a view via
|
|
|
|
normal URL resolution apply view middleware.
|
|
|
|
|
2011-10-07 15:48:30 +00:00
|
|
|
Note that the order of :setting:`MIDDLEWARE_CLASSES` matters. Generally, you
|
|
|
|
can put
|
|
|
|
:class:`~django.contrib.flatpages.middleware.FlatpageFallbackMiddleware` at the
|
|
|
|
end of the list. This means it will run first when processing the response, and
|
|
|
|
ensures that any other response-processing middlewares see the real flatpage
|
|
|
|
response rather than the 404.
|
2005-11-11 04:45:05 +00:00
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
For more on middleware, read the :doc:`middleware docs
|
|
|
|
</topics/http/middleware>`.
|
2005-11-11 04:45:05 +00:00
|
|
|
|
2008-05-28 20:31:03 +00:00
|
|
|
.. admonition:: Ensure that your 404 template works
|
2010-08-28 11:59:14 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Note that the
|
|
|
|
:class:`~django.contrib.flatpages.middleware.FlatpageFallbackMiddleware`
|
|
|
|
only steps in once another view has successfully produced a 404 response.
|
|
|
|
If another view or middleware class attempts to produce a 404 but ends up
|
2012-09-30 21:16:14 +00:00
|
|
|
raising an exception instead, the response will become an HTTP 500
|
2008-08-23 22:25:40 +00:00
|
|
|
("Internal Server Error") and the
|
|
|
|
:class:`~django.contrib.flatpages.middleware.FlatpageFallbackMiddleware`
|
2008-05-28 20:31:03 +00:00
|
|
|
will not attempt to serve a flat page.
|
|
|
|
|
2011-02-18 00:58:34 +00:00
|
|
|
.. currentmodule:: django.contrib.flatpages.models
|
|
|
|
|
2005-11-11 04:45:05 +00: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
|
|
|
|
------------------
|
|
|
|
|
2011-02-18 00:58:34 +00:00
|
|
|
.. class:: FlatPage
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
Flatpages are represented by a standard
|
2010-08-19 19:27:44 +00:00
|
|
|
:doc:`Django model </topics/db/models>`,
|
2008-08-23 22:25:40 +00:00
|
|
|
which lives in `django/contrib/flatpages/models.py`_. You can access
|
2010-08-19 19:27:44 +00:00
|
|
|
flatpage objects via the :doc:`Django database API </topics/db/queries>`.
|
2005-11-11 04:45:05 +00:00
|
|
|
|
2012-05-03 15:42:56 +00:00
|
|
|
.. _django/contrib/flatpages/models.py: https://github.com/django/django/blob/master/django/contrib/flatpages/models.py
|
2005-11-11 04:45:05 +00:00
|
|
|
|
2011-02-18 00:58:34 +00:00
|
|
|
.. currentmodule:: django.contrib.flatpages
|
|
|
|
|
2011-10-07 15:45:52 +00:00
|
|
|
.. admonition:: Check for duplicate flatpage URLs.
|
|
|
|
|
|
|
|
If you add or modify flatpages via your own code, you will likely want to
|
|
|
|
check for duplicate flatpage URLs within the same site. The flatpage form
|
|
|
|
used in the admin performs this validation check, and can be imported from
|
2013-01-01 13:12:42 +00:00
|
|
|
``django.contrib.flatpages.forms.FlatPageForm`` and used in your own
|
2011-10-07 15:45:52 +00:00
|
|
|
views.
|
|
|
|
|
2005-11-11 04:45:05 +00:00
|
|
|
Flatpage templates
|
|
|
|
==================
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
By default, flatpages are rendered via the template
|
2010-03-03 08:34:31 +00:00
|
|
|
:file:`flatpages/default.html`, but you can override that for a
|
|
|
|
particular flatpage: in the admin, a collapsed fieldset titled
|
|
|
|
"Advanced options" (clicking will expand it) contains a field for
|
|
|
|
specifying a template name. If you're creating a flat page via the
|
|
|
|
Python API you can simply set the template name as the field
|
|
|
|
``template_name`` on the ``FlatPage`` object.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
Creating the :file:`flatpages/default.html` template is your responsibility;
|
|
|
|
in your template directory, just create a :file:`flatpages` directory
|
|
|
|
containing a file :file:`default.html`.
|
2005-11-11 04:45:05 +00:00
|
|
|
|
2011-02-18 00:58:34 +00:00
|
|
|
Flatpage templates are passed a single context variable, ``flatpage``,
|
2008-08-23 22:25:40 +00:00
|
|
|
which is the flatpage object.
|
2005-11-11 04:45:05 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Here's a sample :file:`flatpages/default.html` template:
|
2005-11-11 04:45:05 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. code-block:: html+django
|
2005-11-11 04:45:05 +00:00
|
|
|
|
2011-04-20 17:40:53 +00:00
|
|
|
<!DOCTYPE html>
|
2005-11-11 04:45:05 +00:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>{{ flatpage.title }}</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
{{ flatpage.content }}
|
|
|
|
</body>
|
|
|
|
</html>
|
2007-11-14 21:30:18 +00:00
|
|
|
|
|
|
|
Since you're already entering raw HTML into the admin page for a flatpage,
|
|
|
|
both ``flatpage.title`` and ``flatpage.content`` are marked as **not**
|
2008-08-23 22:25:40 +00:00
|
|
|
requiring :ref:`automatic HTML escaping <automatic-html-escaping>` in the
|
|
|
|
template.
|
2010-08-28 11:59:14 +00:00
|
|
|
|
2011-02-18 00:58:34 +00:00
|
|
|
Getting a list of :class:`~django.contrib.flatpages.models.FlatPage` objects in your templates
|
2010-08-28 11:59:14 +00:00
|
|
|
==============================================================================================
|
|
|
|
|
|
|
|
The flatpages app provides a template tag that allows you to iterate
|
|
|
|
over all of the available flatpages on the :ref:`current site
|
|
|
|
<hooking-into-current-site-from-views>`.
|
|
|
|
|
|
|
|
Like all custom template tags, you'll need to :ref:`load its custom
|
|
|
|
tag library <loading-custom-template-libraries>` before you can use
|
|
|
|
it. After loading the library, you can retrieve all current flatpages
|
|
|
|
via the :ttag:`get_flatpages` tag:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
{% load flatpages %}
|
|
|
|
{% get_flatpages as flatpages %}
|
|
|
|
<ul>
|
|
|
|
{% for page in flatpages %}
|
|
|
|
<li><a href="{{ page.url }}">{{ page.title }}</a></li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
.. templatetag:: get_flatpages
|
|
|
|
|
|
|
|
Displaying ``registration_required`` flatpages
|
|
|
|
----------------------------------------------
|
|
|
|
|
|
|
|
By default, the :ttag:`get_flatpages` templatetag will only show
|
2011-02-18 00:58:34 +00:00
|
|
|
flatpages that are marked ``registration_required = False``. If you
|
2010-08-28 11:59:14 +00:00
|
|
|
want to display registration-protected flatpages, you need to specify
|
2013-01-01 13:12:42 +00:00
|
|
|
an authenticated user using a ``for`` clause.
|
2010-08-28 11:59:14 +00:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
{% get_flatpages for someuser as about_pages %}
|
|
|
|
|
|
|
|
If you provide an anonymous user, :ttag:`get_flatpages` will behave
|
|
|
|
the same as if you hadn't provided a user -- i.e., it will only show you
|
|
|
|
public flatpages.
|
|
|
|
|
|
|
|
Limiting flatpages by base URL
|
|
|
|
------------------------------
|
|
|
|
|
|
|
|
An optional argument, ``starts_with``, can be applied to limit the
|
|
|
|
returned pages to those beginning with a particular base URL. This
|
|
|
|
argument may be passed as a string, or as a variable to be resolved
|
|
|
|
from the context.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
{% get_flatpages '/about/' as about_pages %}
|
|
|
|
{% get_flatpages about_prefix as about_pages %}
|
|
|
|
{% get_flatpages '/about/' for someuser as about_pages %}
|