admin_templates.rst 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. ===========================
  2. Customising admin templates
  3. ===========================
  4. In your projects with Wagtail, you may wish to replace elements such as the Wagtail logo within the admin interface with your own branding. This can be done through Django's template inheritance mechanism.
  5. You need to create a ``templates/wagtailadmin/`` folder within one of your apps - this may be an existing one, or a new one created for this purpose, for example, ``dashboard``. This app must be registered in ``INSTALLED_APPS`` before ``wagtail.admin``:
  6. .. code-block:: python
  7. INSTALLED_APPS = (
  8. # ...
  9. 'dashboard',
  10. 'wagtail.core',
  11. 'wagtail.admin',
  12. # ...
  13. )
  14. .. _custom_branding:
  15. Custom branding
  16. ===============
  17. The template blocks that are available to customise the branding in the admin interface are as follows:
  18. ``branding_logo``
  19. -----------------
  20. To replace the default logo, create a template file ``dashboard/templates/wagtailadmin/base.html`` that overrides the block ``branding_logo``:
  21. .. code-block:: html+django
  22. {% extends "wagtailadmin/base.html" %}
  23. {% load static %}
  24. {% block branding_logo %}
  25. <img src="{% static 'images/custom-logo.svg' %}" alt="Custom Project" width="80" />
  26. {% endblock %}
  27. The logo also appears on the admin 404 error page; to replace it there too, create a template file ``dashboard/templates/wagtailadmin/404.html`` that overrides the ``branding_logo`` block.
  28. The logo also appears on the wagtail userbar; to replace it there too, create a template file ``dashboard/templates/wagtailadmin/userbar/base.html`` that overwrites the ``branding_logo`` block.
  29. ``branding_favicon``
  30. --------------------
  31. To replace the favicon displayed when viewing admin pages, create a template file ``dashboard/templates/wagtailadmin/admin_base.html`` that overrides the block ``branding_favicon``:
  32. .. code-block:: html+django
  33. {% extends "wagtailadmin/admin_base.html" %}
  34. {% load static %}
  35. {% block branding_favicon %}
  36. <link rel="shortcut icon" href="{% static 'images/favicon.ico' %}" />
  37. {% endblock %}
  38. ``branding_title``
  39. ------------------
  40. To replace the title prefix (which is 'Wagtail' by default), create a template file ``dashboard/templates/wagtailadmin/admin_base.html`` that overrides the block ``branding_title``:
  41. .. code-block:: html+django
  42. {% extends "wagtailadmin/admin_base.html" %}
  43. {% block branding_title %}Frank's CMS{% endblock %}
  44. ``branding_login``
  45. ------------------
  46. To replace the login message, create a template file ``dashboard/templates/wagtailadmin/login.html`` that overrides the block ``branding_login``:
  47. .. code-block:: html+django
  48. {% extends "wagtailadmin/login.html" %}
  49. {% block branding_login %}Sign in to Frank's Site{% endblock %}
  50. ``branding_welcome``
  51. --------------------
  52. To replace the welcome message on the dashboard, create a template file ``dashboard/templates/wagtailadmin/home.html`` that overrides the block ``branding_welcome``:
  53. .. code-block:: html+django
  54. {% extends "wagtailadmin/home.html" %}
  55. {% block branding_welcome %}Welcome to Frank's Site{% endblock %}
  56. .. _custom_user_interface_colors:
  57. Custom user interface colors
  58. ============================
  59. .. warning::
  60. The default Wagtail colors conform to the WCAG2.1 AA level color contrast requirements. When customizing the admin colors you should test the contrast using tools like `Axe <https://www.deque.com/axe/browser-extensions/>`_.
  61. To customize the primary color used in the admin user interface, inject a CSS file using the hook :ref:`insert_global_admin_css` and override the variables within the ``:root`` selector:
  62. .. code-block:: text
  63. :root {
  64. --color-primary-hue: 25;
  65. }
  66. ``color-primary`` is an `hsl color <https://en.wikipedia.org/wiki/HSL_and_HSV>`_ composed of 3 CSS variables - ``--color-primary-hue`` (0-360 with no unit), ``--color-primary-saturation`` (a percentage), and ``--color-primary-lightness`` (also a percentage). Separating the color into 3 allows us to calculate variations on the color to use alongside the primary color. If needed, you can also control those variations manually by setting ``hue``, ``saturation``, and ``lightness`` variables for the following colors: ``color-primary-darker``, ``color-primary-dark``, ``color-primary-lighter``, ``color-primary-light``, ``color-input-focus``, and ``color-input-focus-border``:
  67. .. code-block:: text
  68. :root {
  69. --color-primary-hue: 25;
  70. --color-primary-saturation: 100%;
  71. --color-primary-lightness: 25%;
  72. --color-primary-darker-hue: 24;
  73. --color-primary-darker-saturation: 100%;
  74. --color-primary-darker-lightness: 20%;
  75. --color-primary-dark-hue: 23;
  76. --color-primary-dark-saturation: 100%;
  77. --color-primary-dark-lightness: 15%;
  78. }
  79. If instead you intend to set all available colors, you can use any valid css colors:
  80. .. code-block:: text
  81. :root {
  82. --color-primary: mediumaquamarine;
  83. --color-primary-darker: rebeccapurple;
  84. --color-primary-dark: hsl(330, 100%, 70%);
  85. --color-primary-lighter: azure;
  86. --color-primary-light: aliceblue;
  87. --color-input-focus: rgb(204, 0, 102);
  88. --color-input-focus-border: #4d0026;
  89. }
  90. Specifying a site or page in the branding
  91. =========================================
  92. The admin interface has a number of variables available to the renderer context that can be used to customize the branding in the admin page. These can be useful for customizing the dashboard on a multitenanted Wagtail installation:
  93. ``root_page``
  94. -------------
  95. Returns the highest explorable page object for the currently logged in user. If the user has no explore rights, this will default to ``None``.
  96. ``root_site``
  97. -------------
  98. Returns the name on the site record for the above root page.
  99. ``site_name``
  100. -------------
  101. Returns the value of ``root_site``, unless it evaluates to ``None``. In that case, it will return the value of ``settings.WAGTAIL_SITE_NAME``.
  102. To use these variables, create a template file ``dashboard/templates/wagtailadmin/home.html``, just as if you were overriding one of the template blocks in the dashboard, and use them as you would any other Django template variable:
  103. .. code-block:: html+django
  104. {% extends "wagtailadmin/home.html" %}
  105. {% block branding_welcome %}Welcome to the Admin Homepage for {{ root_site }}{% endblock %}
  106. Extending the login form
  107. ========================
  108. To add extra controls to the login form, create a template file ``dashboard/templates/wagtailadmin/login.html``.
  109. ``above_login`` and ``below_login``
  110. -----------------------------------
  111. To add content above or below the login form, override these blocks:
  112. .. code-block:: html+django
  113. {% extends "wagtailadmin/login.html" %}
  114. {% block above_login %} If you are not Frank you should not be here! {% endblock %}
  115. ``fields``
  116. ----------
  117. To add extra fields to the login form, override the ``fields`` block. You will need to add ``{{ block.super }}`` somewhere in your block to include the username and password fields:
  118. .. code-block:: html+django
  119. {% extends "wagtailadmin/login.html" %}
  120. {% block fields %}
  121. {{ block.super }}
  122. <li class="full">
  123. <div class="field iconfield">
  124. Two factor auth token
  125. <div class="input icon-key">
  126. <input type="text" name="two-factor-auth">
  127. </div>
  128. </div>
  129. </li>
  130. {% endblock %}
  131. ``submit_buttons``
  132. ------------------
  133. To add extra buttons to the login form, override the ``submit_buttons`` block. You will need to add ``{{ block.super }}`` somewhere in your block to include the sign in button:
  134. .. code-block:: html+django
  135. {% extends "wagtailadmin/login.html" %}
  136. {% block submit_buttons %}
  137. {{ block.super }}
  138. <a href="{% url 'signup' %}"><button type="button" class="button">{% trans 'Sign up' %}</button></a>
  139. {% endblock %}
  140. ``login_form``
  141. --------------
  142. To completely customise the login form, override the ``login_form`` block. This block wraps the whole contents of the ``<form>`` element:
  143. .. code-block:: html+django
  144. {% extends "wagtailadmin/login.html" %}
  145. {% block login_form %}
  146. <p>Some extra form content</p>
  147. {{ block.super }}
  148. {% endblock %}
  149. .. _extending_clientside_components:
  150. Extending client-side components
  151. ================================
  152. Some of Wagtail’s admin interface is written as client-side JavaScript with `React <https://reactjs.org/>`_.
  153. In order to customise or extend those components, you may need to use React too, as well as other related libraries.
  154. To make this easier, Wagtail exposes its React-related dependencies as global variables within the admin. Here are the available packages:
  155. .. code-block:: javascript
  156. // 'focus-trap-react'
  157. window.FocusTrapReact;
  158. // 'react'
  159. window.React;
  160. // 'react-dom'
  161. window.ReactDOM;
  162. // 'react-transition-group/CSSTransitionGroup'
  163. window.CSSTransitionGroup;
  164. Wagtail also exposes some of its own React components. You can reuse:
  165. .. code-block:: javascript
  166. window.wagtail.components.Icon;
  167. window.wagtail.components.Portal;
  168. Pages containing rich text editors also have access to:
  169. .. code-block:: javascript
  170. // 'draft-js'
  171. window.DraftJS;
  172. // 'draftail'
  173. window.Draftail;
  174. // Wagtail’s Draftail-related APIs and components.
  175. window.draftail;
  176. window.draftail.ModalWorkflowSource;
  177. window.draftail.ImageModalWorkflowSource;
  178. window.draftail.EmbedModalWorkflowSource;
  179. window.draftail.LinkModalWorkflowSource;
  180. window.draftail.DocumentModalWorkflowSource;
  181. window.draftail.Tooltip;
  182. window.draftail.TooltipEntity;