custom_account_settings.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. Customising the user account settings form
  2. ==========================================
  3. This document describes how to customise the user account settings form that can be found by clicking "Account settings"
  4. at the bottom of the main menu.
  5. Adding new panels
  6. -----------------
  7. Each panel on this form is a separate model form which can operate on an instance of either the user model, or the
  8. :class:`~wagtail.users.models.UserProfile` model.
  9. Basic example
  10. ~~~~~~~~~~~~~
  11. Here is an example of how to add a new form that operates on the user model:
  12. .. code-block:: python
  13. # forms.py
  14. from django import forms
  15. from django.contrib.auth import get_user_model
  16. class CustomSettingsForm(forms.ModelForm):
  17. class Meta:
  18. model = get_user_model()
  19. fields = [...]
  20. .. code-block:: python
  21. # hooks.py
  22. from wagtail.admin.views.account import BaseSettingsPanel
  23. from wagtail.core import hooks
  24. from .forms import CustomSettingsForm
  25. @hooks.register('register_account_settings_panel')
  26. class CustomSettingsPanel(BaseSettingsPanel):
  27. name = 'custom'
  28. title = "My custom settings"
  29. order = 500
  30. form_class = CustomSettingsForm
  31. form_object = 'user'
  32. The attributes are as follows:
  33. - ``name`` - A unique name for the panel. All form fields are prefixed with this name, so it must be lowercase and cannot contain symbols -
  34. - ``title`` - The heading that is displayed to the user
  35. - ``order`` - Used to order panels on a tab. The builtin Wagtail panels start at ``100`` and increase by ``100`` for each panel.
  36. - ``form_class`` - A ``ModelForm`` subclass that operates on a user or a profile
  37. - ``form_object`` - Set to ``user`` to operate on the user, and ``profile`` to operate on the profile
  38. - ``tab`` (optional) - Set which tab the panel appears on.
  39. - ``template_name`` (optional) - Override the default template used for rendering the panel
  40. Operating on the ``UserProfile`` model
  41. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  42. To add a panel that alters data on the user's :class:`~wagtail.users.models.UserProfile` instance, set ``form_object`` to ``'profile'``:
  43. .. code-block:: python
  44. # forms.py
  45. from django import forms
  46. from wagtail.users.models import UserProfile
  47. class CustomProfileSettingsForm(forms.ModelForm):
  48. class Meta:
  49. model = UserProfile
  50. fields = [...]
  51. .. code-block:: python
  52. # hooks.py
  53. from wagtail.admin.views.account import BaseSettingsPanel
  54. from wagtail.core import hooks
  55. from .forms import CustomProfileSettingsForm
  56. @hooks.register('register_account_settings_panel')
  57. class CustomSettingsPanel(BaseSettingsPanel):
  58. name = 'custom'
  59. title = "My custom settings"
  60. order = 500
  61. form_class = CustomProfileSettingsForm
  62. form_object = 'profile'
  63. Creating new tabs
  64. ~~~~~~~~~~~~~~~~~
  65. You can define a new tab using the ``SettingsTab`` class:
  66. .. code-block:: python
  67. # hooks.py
  68. from wagtail.admin.views.account import BaseSettingsPanel, SettingsTab
  69. from wagtail.core import hooks
  70. from .forms import CustomSettingsForm
  71. custom_tab = SettingsTab('custom', "Custom settings", order=300)
  72. @hooks.register('register_account_settings_panel')
  73. class CustomSettingsPanel(BaseSettingsPanel):
  74. name = 'custom'
  75. title = "My custom settings"
  76. tab = custom_tab
  77. order = 100
  78. form_class = CustomSettingsForm
  79. ``SettingsTab`` takes three arguments:
  80. - ``name`` - A slug to use for the tab (this is placed after the ``#`` when linking to a tab)
  81. - ``title`` - The display name of the title
  82. - ``order`` - The order of the tab. The builtin Wagtail tabs start at ``100`` and increase by ``100`` for each tab
  83. Customising the template
  84. ~~~~~~~~~~~~~~~~~~~~~~~~
  85. You can provide a custom template for the panel by specifying a template name:
  86. .. code-block:: python
  87. # hooks.py
  88. from wagtail.admin.views.account import BaseSettingsPanel
  89. from wagtail.core import hooks
  90. from .forms import CustomSettingsForm
  91. @hooks.register('register_account_settings_panel')
  92. class CustomSettingsPanel(BaseSettingsPanel):
  93. name = 'custom'
  94. title = "My custom settings"
  95. order = 500
  96. form_class = CustomSettingsForm
  97. template_name = 'myapp/admin/custom_settings.html'
  98. .. code-block:: html+Django
  99. {# templates/myapp/admin/custom_settings.html #}
  100. {# This is the default template Wagtail uses, which just renders the form #}
  101. <ul class="fields">
  102. {% for field in form %}
  103. {% include "wagtailadmin/shared/field_as_li.html" with field=field %}
  104. {% endfor %}
  105. </ul>