2
0

privacy.rst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. .. _private_pages:
  2. Private pages
  3. =============
  4. Users with publish permission on a page can set it to be private by clicking the 'Privacy' control in the top right corner of the page explorer or editing interface, and setting a password. Users visiting this page, or any of its subpages, will be prompted to enter a password before they can view the page.
  5. Private pages work on Wagtail out of the box - the site implementer does not need to do anything to set them up. However, the default "password required" form is only a bare-bones HTML page, and site implementers may wish to replace this with a page customised to their site design.
  6. Setting up a global "password required" page
  7. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. By setting ``PASSWORD_REQUIRED_TEMPLATE`` in your Django settings file, you can specify the path of a template which will be used for all "password required" forms on the site (except for page types that specifically override it - see below):
  9. .. code-block:: python
  10. PASSWORD_REQUIRED_TEMPLATE = 'myapp/password_required.html'
  11. This template will receive the same set of context variables that the blocked page would pass to its own template via ``get_context()`` - including ``page`` to refer to the page object itself - plus the following additional variables (which override any of the page's own context variables of the same name):
  12. - **form** - A Django form object for the password prompt; this will contain a field named ``password`` as its only visible field. A number of hidden fields may also be present, so the page must loop over ``form.hidden_fields`` if not using one of Django's rendering helpers such as ``form.as_p``.
  13. - **action_url** - The URL that the password form should be submitted to, as a POST request.
  14. A basic template suitable for use as ``PASSWORD_REQUIRED_TEMPLATE`` might look like this:
  15. .. code-block:: html+django
  16. <!DOCTYPE HTML>
  17. <html>
  18. <head>
  19. <title>Password required</title>
  20. </head>
  21. <body>
  22. <h1>Password required</h1>
  23. <p>You need a password to access this page.</p>
  24. <form action="{{ action_url }}" method="POST">
  25. {% csrf_token %}
  26. {{ form.non_field_errors }}
  27. <div>
  28. {{ form.password.errors }}
  29. {{ form.password.label_tag }}
  30. {{ form.password }}
  31. </div>
  32. {% for field in form.hidden_fields %}
  33. {{ field }}
  34. {% endfor %}
  35. <input type="submit" value="Continue" />
  36. </form>
  37. </body>
  38. </html>
  39. Setting a "password required" page for a specific page type
  40. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  41. The attribute ``password_required_template`` can be defined on a page model to use a custom template for the "password required" view, for that page type only. For example, if a site had a page type for displaying embedded videos along with a description, it might choose to use a custom "password required" template that displays the video description as usual, but shows the password form in place of the video embed.
  42. .. code-block:: python
  43. class VideoPage(Page):
  44. ...
  45. password_required_template = 'video/password_required.html'