2
0

settings.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. .. _settings:
  2. =============
  3. Site settings
  4. =============
  5. You can define settings for your site that are editable by administrators in the Wagtail admin. These settings can be accessed in code, as well as in templates.
  6. To use these settings, you must add ``wagtail.contrib.settings`` to your ``INSTALLED_APPS``:
  7. .. code-block:: python
  8. INSTALLED_APPS += [
  9. 'wagtail.contrib.settings',
  10. ]
  11. Defining settings
  12. =================
  13. Create a model that inherits from ``BaseSetting``, and register it using the ``register_setting`` decorator:
  14. .. code-block:: python
  15. from wagtail.contrib.settings.models import BaseSetting, register_setting
  16. @register_setting
  17. class SocialMediaSettings(BaseSetting):
  18. facebook = models.URLField(
  19. help_text='Your Facebook page URL')
  20. instagram = models.CharField(
  21. max_length=255, help_text='Your Instagram username, without the @')
  22. trip_advisor = models.URLField(
  23. help_text='Your Trip Advisor page URL')
  24. youtube = models.URLField(
  25. help_text='Your YouTube channel or user account URL')
  26. A 'Social media settings' link will appear in the Wagtail admin 'Settings' menu.
  27. .. _edit_handlers_settings:
  28. Edit handlers
  29. -------------
  30. Settings use edit handlers much like the rest of Wagtail. Add a ``panels`` setting to your model defining all the edit handlers required:
  31. .. code-block:: python
  32. @register_setting
  33. class ImportantPages(BaseSetting):
  34. donate_page = models.ForeignKey(
  35. 'wagtailcore.Page', null=True, on_delete=models.SET_NULL)
  36. sign_up_page = models.ForeignKey(
  37. 'wagtailcore.Page', null=True, on_delete=models.SET_NULL)
  38. panels = [
  39. PageChooserPanel('donate_page'),
  40. PageChooserPanel('sign_up_page'),
  41. ]
  42. You can also customize the editor handlers :ref:`like you would do for Page model <customising_the_tabbed_interface>`
  43. with a custom ``edit_handler`` attribute:
  44. .. code-block:: python
  45. from wagtail.wagtailadmin.edit_handlers import TabbedInterface, ObjectList
  46. @register_setting
  47. class MySettings(BaseSetting):
  48. # ...
  49. first_tab_panels = [
  50. FieldPanel('field_1'),
  51. ]
  52. second_tab_panels = [
  53. FieldPanel('field_2'),
  54. ]
  55. edit_handler = TabbedInterface([
  56. ObjectList(first_tab_panels, heading='First tab'),
  57. ObjectList(second_tab_panels, heading='Second tab'),
  58. ])
  59. Appearance
  60. ----------
  61. You can change the label used in the menu by changing the `verbose_name <https://docs.djangoproject.com/en/dev/ref/models/options/#verbose-name>`_ of your model.
  62. You can add an icon to the menu by passing an 'icon' argument to the ``register_setting`` decorator:
  63. .. code-block:: python
  64. @register_setting(icon='placeholder')
  65. class SocialMediaSettings(BaseSetting):
  66. class Meta:
  67. verbose_name = 'social media accounts'
  68. ...
  69. For a list of all available icons, please see the :ref:`styleguide`.
  70. Using the settings
  71. ==================
  72. Settings are designed to be used both in Python code, and in templates.
  73. Using in Python
  74. ---------------
  75. If access to a setting is required in the code, the :func:`~wagtail.contrib.settings.models.BaseSetting.for_site` method will retrieve the setting for the supplied site:
  76. .. code-block:: python
  77. def view(request):
  78. social_media_settings = SocialMediaSettings.for_site(request.site)
  79. ...
  80. Using in Django templates
  81. -------------------------
  82. Add the ``settings`` context processor to your settings:
  83. .. code-block:: python
  84. TEMPLATES = [
  85. {
  86. ...
  87. 'OPTIONS': {
  88. 'context_processors': [
  89. ...
  90. 'wagtail.contrib.settings.context_processors.settings',
  91. ]
  92. }
  93. }
  94. ]
  95. Then access the settings through ``{{ settings }}``:
  96. .. code-block:: html+django
  97. {{ settings.app_label.SocialMediaSettings.instagram }}
  98. .. note:: Replace ``app_label`` with the label of the app containing your settings model.
  99. If you are not in a ``RequestContext``, then context processors will not have run, and the ``settings`` variable will not be availble. To get the ``settings``, use the provided ``{% get_settings %}`` template tag. If a ``request`` is in the template context, but for some reason it is not a ``RequestContext``, just use ``{% get_settings %}``:
  100. .. code-block:: html+django
  101. {% load wagtailsettings_tags %}
  102. {% get_settings %}
  103. {{ settings.app_label.SocialMediaSettings.instagram }}
  104. If there is no ``request`` available in the template at all, you can use the settings for the default Wagtail site instead:
  105. .. code-block:: html+django
  106. {% load wagtailsettings_tags %}
  107. {% get_settings use_default_site=True %}
  108. {{ settings.app_label.SocialMediaSettings.instagram }}
  109. .. note:: You can not reliably get the correct settings instance for the current site from this template tag if the request object is not available. This is only relevant for multisite instances of Wagtail.
  110. .. _settings_tag_jinja2:
  111. Using in Jinja2 templates
  112. -------------------------
  113. Add ``wagtail.contrib.settings.jinja2tags.settings`` extension to your Jinja2 settings:
  114. .. code-block:: python
  115. TEMPLATES = [
  116. # ...
  117. {
  118. 'BACKEND': 'django.template.backends.jinja2.Jinja2',
  119. 'APP_DIRS': True,
  120. 'OPTIONS': {
  121. 'extensions': [
  122. # ...
  123. 'wagtail.contrib.settings.jinja2tags.settings',
  124. ],
  125. },
  126. }
  127. ]
  128. Then access the settings through the ``settings()`` template function:
  129. .. code-block:: html+jinja
  130. {{ settings("app_label.SocialMediaSettings").twitter }}
  131. .. note:: Replace ``app_label`` with the label of the app containing your settings model.
  132. This will look for a ``request`` variable in the template context, and find the correct site to use from that. If for some reason you do not have a ``request`` available, you can instead use the settings defined for the default site:
  133. .. code-block:: html+jinja
  134. {{ settings("app_label.SocialMediaSettings", use_default_site=True).instagram }}
  135. You can store the settings instance in a variable to save some typing, if you have to use multiple values from one model:
  136. .. code-block:: html+jinja
  137. {% with social_settings=settings("app_label.SocialMediaSettings") %}
  138. Follow us on Twitter at @{{ social_settings.twitter }},
  139. or Instagram at @{{ social_settings.Instagram }}.
  140. {% endwith %}
  141. Or, alternately, using the ``set`` tag:
  142. .. code-block:: html+jinja
  143. {% set social_settings=settings("app_label.SocialMediaSettings") %}