flatpages.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. .. _ref-contrib-flatpages:
  2. =================
  3. The flatpages app
  4. =================
  5. .. module:: django.contrib.flatpages
  6. :synopsis: A framework for managing simple ?flat? HTML content in a database.
  7. Django comes with an optional "flatpages" application. It lets you store simple
  8. "flat" HTML content in a database and handles the management for you via
  9. Django's admin interface and a Python API.
  10. A flatpage is a simple object with a URL, title and content. Use it for
  11. one-off, special-case pages, such as "About" or "Privacy Policy" pages, that
  12. you want to store in a database but for which you don't want to develop a
  13. 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.
  16. .. versionadded:: 1.0
  17. The content field may optionally be left blank if you prefer to put your
  18. content in a custom template.
  19. Here are some examples of flatpages on Django-powered sites:
  20. * http://www.everyblock.com/about/
  21. * http://www.lawrence.com/about/contact/
  22. Installation
  23. ============
  24. To install the flatpages app, follow these steps:
  25. 1. Install the :mod:`sites framework <django.contrib.sites>` by adding
  26. ``'django.contrib.sites'`` to your :setting:`INSTALLED_APPS` setting,
  27. if it's not already in there.
  28. Also make sure you've correctly set :setting:`SITE_ID` to the ID of the
  29. site the settings file represents. This will usually be ``1`` (i.e.
  30. ``SITE_ID = 1``, but if you're not using the sites framework to manage
  31. multiple sites, it could be the ID of a different site.
  32. 2. Add ``'django.contrib.flatpages'`` to your :setting:`INSTALLED_APPS`
  33. setting.
  34. 3. Add ``'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'``
  35. to your :setting:`MIDDLEWARE_CLASSES` setting.
  36. 4. Run the command :djadmin:`manage.py syncdb <syncdb>`.
  37. How it works
  38. ============
  39. ``manage.py syncdb`` creates two tables in your database: ``django_flatpage``
  40. and ``django_flatpage_sites``. ``django_flatpage`` is a simple lookup table
  41. that simply maps a URL to a title and bunch of text content.
  42. ``django_flatpage_sites`` associates a flatpage with a site.
  43. The :class:`~django.contrib.flatpages.middleware.FlatpageFallbackMiddleware`
  44. does all of the work. Each time any Django application raises a 404 error, this
  45. middleware checks the flatpages database for the requested URL as a last resort.
  46. Specifically, it checks for a flatpage with the given URL with a site ID that
  47. corresponds to the :setting:`SITE_ID` setting.
  48. If it finds a match, it follows this algorithm:
  49. * If the flatpage has a custom template, it loads that template. Otherwise,
  50. it loads the template :file:`flatpages/default.html`.
  51. * It passes that template a single context variable, :data:`flatpage`, which
  52. is the flatpage object. It uses
  53. :class:`~django.template.context.RequestContext` in rendering the
  54. template.
  55. If it doesn't find a match, the request continues to be processed as usual.
  56. The middleware only gets activated for 404s -- not for 500s or responses of any
  57. other status code.
  58. Note that the order of :setting:`MIDDLEWARE_CLASSES` matters. Generally, you can
  59. put :class:`~django.contrib.flatpages.middleware.FlatpageFallbackMiddleware` at
  60. the end of the list, because it's a last resort.
  61. For more on middleware, read the :ref:`middleware docs
  62. <topics-http-middleware>`.
  63. .. admonition:: Ensure that your 404 template works
  64. Note that the
  65. :class:`~django.contrib.flatpages.middleware.FlatpageFallbackMiddleware`
  66. only steps in once another view has successfully produced a 404 response.
  67. If another view or middleware class attempts to produce a 404 but ends up
  68. raising an exception instead (such as a ``TemplateDoesNotExist``
  69. exception if your site does not have an appropriate template to
  70. use for HTTP 404 responses), the response will become an HTTP 500
  71. ("Internal Server Error") and the
  72. :class:`~django.contrib.flatpages.middleware.FlatpageFallbackMiddleware`
  73. will not attempt to serve a flat page.
  74. How to add, change and delete flatpages
  75. =======================================
  76. Via the admin interface
  77. -----------------------
  78. If you've activated the automatic Django admin interface, you should see a
  79. "Flatpages" section on the admin index page. Edit flatpages as you edit any
  80. other object in the system.
  81. Via the Python API
  82. ------------------
  83. .. class:: models.FlatPage
  84. Flatpages are represented by a standard
  85. :ref:`Django model <topics-db-models>`,
  86. which lives in `django/contrib/flatpages/models.py`_. You can access
  87. flatpage objects via the :ref:`Django database API <topics-db-queries>`.
  88. .. _django/contrib/flatpages/models.py: http://code.djangoproject.com/browser/django/trunk/django/contrib/flatpages/models.py
  89. Flatpage templates
  90. ==================
  91. By default, flatpages are rendered via the template
  92. :file:`flatpages/default.html`, but you can override that for a particular
  93. flatpage.
  94. Creating the :file:`flatpages/default.html` template is your responsibility;
  95. in your template directory, just create a :file:`flatpages` directory
  96. containing a file :file:`default.html`.
  97. Flatpage templates are passed a single context variable, :data:`flatpage`,
  98. which is the flatpage object.
  99. Here's a sample :file:`flatpages/default.html` template:
  100. .. code-block:: html+django
  101. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
  102. "http://www.w3.org/TR/REC-html40/loose.dtd">
  103. <html>
  104. <head>
  105. <title>{{ flatpage.title }}</title>
  106. </head>
  107. <body>
  108. {{ flatpage.content }}
  109. </body>
  110. </html>
  111. Since you're already entering raw HTML into the admin page for a flatpage,
  112. both ``flatpage.title`` and ``flatpage.content`` are marked as **not**
  113. requiring :ref:`automatic HTML escaping <automatic-html-escaping>` in the
  114. template.