flatpages.txt 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. =================
  2. The flatpages app
  3. =================
  4. .. module:: django.contrib.flatpages
  5. :synopsis: A framework for managing simple ?flat? HTML content in a database.
  6. Django comes with an optional "flatpages" application. It lets you store simple
  7. "flat" HTML content in a database and handles the management for you via
  8. 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.
  13. A flatpage can use a custom template or a default, systemwide flatpage
  14. template. It can be associated with one, or multiple, sites.
  15. The content field may optionally be left blank if you prefer to put your
  16. content in a custom template.
  17. Here are some examples of flatpages on Django-powered sites:
  18. * http://www.lawrence.com/about/contact/
  19. * http://www2.ljworld.com/site/rules/
  20. Installation
  21. ============
  22. To install the flatpages app, follow these steps:
  23. 1. Install the :mod:`sites framework <django.contrib.sites>` by adding
  24. ``'django.contrib.sites'`` to your :setting:`INSTALLED_APPS` setting,
  25. if it's not already in there.
  26. Also make sure you've correctly set :setting:`SITE_ID` to the ID of the
  27. site the settings file represents. This will usually be ``1`` (i.e.
  28. ``SITE_ID = 1``, but if you're using the sites framework to manage
  29. multiple sites, it could be the ID of a different site.
  30. 2. Add ``'django.contrib.flatpages'`` to your :setting:`INSTALLED_APPS`
  31. setting.
  32. 3. Add ``'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'``
  33. to your :setting:`MIDDLEWARE_CLASSES` setting.
  34. 4. Run the command :djadmin:`manage.py syncdb <syncdb>`.
  35. How it works
  36. ============
  37. ``manage.py syncdb`` creates two tables in your database: ``django_flatpage``
  38. and ``django_flatpage_sites``. ``django_flatpage`` is a simple lookup table
  39. that simply maps a URL to a title and bunch of text content.
  40. ``django_flatpage_sites`` associates a flatpage with a site.
  41. The :class:`~django.contrib.flatpages.middleware.FlatpageFallbackMiddleware`
  42. does all of the work. Each time any Django application raises a 404 error, this
  43. middleware checks the flatpages database for the requested URL as a last resort.
  44. Specifically, it checks for a flatpage with the given URL with a site ID that
  45. corresponds to the :setting:`SITE_ID` setting.
  46. If it finds a match, it follows this algorithm:
  47. * If the flatpage has a custom template, it loads that template. Otherwise,
  48. it loads the template :file:`flatpages/default.html`.
  49. * It passes that template a single context variable, :data:`flatpage`, which
  50. is the flatpage object. It uses
  51. :class:`~django.template.context.RequestContext` in rendering the
  52. template.
  53. If it doesn't find a match, the request continues to be processed as usual.
  54. The middleware only gets activated for 404s -- not for 500s or responses of any
  55. other status code.
  56. .. admonition:: Flatpages will not apply view middleware
  57. Because the ``FlatpageFallbackMiddleware`` is applied only after
  58. URL resolution has failed and produced a 404, the response it
  59. returns will not apply any :ref:`view middleware <view-middleware>`
  60. methods. Only requests which are successfully routed to a view via
  61. normal URL resolution apply view middleware.
  62. Note that the order of :setting:`MIDDLEWARE_CLASSES` matters. Generally, you can
  63. put :class:`~django.contrib.flatpages.middleware.FlatpageFallbackMiddleware` at
  64. the end of the list, because it's a last resort.
  65. For more on middleware, read the :doc:`middleware docs
  66. </topics/http/middleware>`.
  67. .. admonition:: Ensure that your 404 template works
  68. Note that the
  69. :class:`~django.contrib.flatpages.middleware.FlatpageFallbackMiddleware`
  70. only steps in once another view has successfully produced a 404 response.
  71. If another view or middleware class attempts to produce a 404 but ends up
  72. raising an exception instead (such as a ``TemplateDoesNotExist``
  73. exception if your site does not have an appropriate template to
  74. use for HTTP 404 responses), the response will become an HTTP 500
  75. ("Internal Server Error") and the
  76. :class:`~django.contrib.flatpages.middleware.FlatpageFallbackMiddleware`
  77. will not attempt to serve a flat page.
  78. How to add, change and delete flatpages
  79. =======================================
  80. Via the admin interface
  81. -----------------------
  82. If you've activated the automatic Django admin interface, you should see a
  83. "Flatpages" section on the admin index page. Edit flatpages as you edit any
  84. other object in the system.
  85. Via the Python API
  86. ------------------
  87. .. class:: models.FlatPage
  88. Flatpages are represented by a standard
  89. :doc:`Django model </topics/db/models>`,
  90. which lives in `django/contrib/flatpages/models.py`_. You can access
  91. flatpage objects via the :doc:`Django database API </topics/db/queries>`.
  92. .. _django/contrib/flatpages/models.py: http://code.djangoproject.com/browser/django/trunk/django/contrib/flatpages/models.py
  93. Flatpage templates
  94. ==================
  95. By default, flatpages are rendered via the template
  96. :file:`flatpages/default.html`, but you can override that for a
  97. particular flatpage: in the admin, a collapsed fieldset titled
  98. "Advanced options" (clicking will expand it) contains a field for
  99. specifying a template name. If you're creating a flat page via the
  100. Python API you can simply set the template name as the field
  101. ``template_name`` on the ``FlatPage`` object.
  102. Creating the :file:`flatpages/default.html` template is your responsibility;
  103. in your template directory, just create a :file:`flatpages` directory
  104. containing a file :file:`default.html`.
  105. Flatpage templates are passed a single context variable, :data:`flatpage`,
  106. which is the flatpage object.
  107. Here's a sample :file:`flatpages/default.html` template:
  108. .. code-block:: html+django
  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. Since you're already entering raw HTML into the admin page for a flatpage,
  120. both ``flatpage.title`` and ``flatpage.content`` are marked as **not**
  121. requiring :ref:`automatic HTML escaping <automatic-html-escaping>` in the
  122. template.
  123. Getting a list of :class:`~django.contrib.flatpages.models.Flatpage` objects in your templates
  124. ==============================================================================================
  125. .. versionadded:: 1.3
  126. The flatpages app provides a template tag that allows you to iterate
  127. over all of the available flatpages on the :ref:`current site
  128. <hooking-into-current-site-from-views>`.
  129. Like all custom template tags, you'll need to :ref:`load its custom
  130. tag library <loading-custom-template-libraries>` before you can use
  131. it. After loading the library, you can retrieve all current flatpages
  132. via the :ttag:`get_flatpages` tag:
  133. .. code-block:: html+django
  134. {% load flatpages %}
  135. {% get_flatpages as flatpages %}
  136. <ul>
  137. {% for page in flatpages %}
  138. <li><a href="{{ page.url }}">{{ page.title }}</a></li>
  139. {% endfor %}
  140. </ul>
  141. .. templatetag:: get_flatpages
  142. Displaying ``registration_required`` flatpages
  143. ----------------------------------------------
  144. By default, the :ttag:`get_flatpages` templatetag will only show
  145. flatpages that are marked :attr:`registration_required`\=False. If you
  146. want to display registration-protected flatpages, you need to specify
  147. an authenticated user using a``for`` clause.
  148. For example:
  149. .. code-block:: html+django
  150. {% get_flatpages for someuser as about_pages %}
  151. If you provide an anonymous user, :ttag:`get_flatpages` will behave
  152. the same as if you hadn't provided a user -- i.e., it will only show you
  153. public flatpages.
  154. Limiting flatpages by base URL
  155. ------------------------------
  156. An optional argument, ``starts_with``, can be applied to limit the
  157. returned pages to those beginning with a particular base URL. This
  158. argument may be passed as a string, or as a variable to be resolved
  159. from the context.
  160. For example:
  161. .. code-block:: html+django
  162. {% get_flatpages '/about/' as about_pages %}
  163. {% get_flatpages about_prefix as about_pages %}
  164. {% get_flatpages '/about/' for someuser as about_pages %}