settings.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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
  28. -------------
  29. Settings use edit handlers much like the rest of Wagtail. Add a ``panels`` setting to your model defining all the edit handlers required:
  30. .. code-block:: python
  31. @register_setting
  32. class ImportantPages(BaseSetting):
  33. donate_page = models.ForeignKey(
  34. 'wagtailcore.Page', null=True, on_delete=models.SET_NULL)
  35. sign_up_page = models.ForeignKey(
  36. 'wagtailcore.Page', null=True, on_delete=models.SET_NULL)
  37. panels = [
  38. PageChooserPanel('donate_page'),
  39. PageChooserPanel('sign_up_page'),
  40. ]
  41. Appearance
  42. ----------
  43. 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.
  44. You can add an icon to the menu by passing an 'icon' argument to the ``register_setting`` decorator:
  45. .. code-block:: python
  46. @register_setting(icon='icon-placeholder')
  47. class SocialMediaSettings(BaseSetting):
  48. class Meta:
  49. verbose_name = 'Social media accounts'
  50. ...
  51. For a list of all available icons, please see the :ref:`styleguide`.
  52. Using the settings
  53. ==================
  54. Settings are designed to be used both in Python code, and in templates.
  55. Using in Python
  56. ---------------
  57. 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:
  58. .. code-block:: python
  59. def view(request):
  60. social_media_settings = SocialMediaSettings.for_site(request.site)
  61. ...
  62. Using in templates
  63. ------------------
  64. Add the ``request`` and ``settings`` context processors to your settings:
  65. .. code-block:: python
  66. from django.conf import global_settings
  67. TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + [
  68. 'django.core.context_processors.request',
  69. 'wagtail.contrib.settings.context_processors.settings',
  70. ]
  71. Then access the settings through ``{{ settings }}``:
  72. .. code-block:: html+django
  73. {{ settings.app_label.SocialMediaSettings.instagram }}
  74. 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 %}``:
  75. .. code-block:: html+django
  76. {% load wagtailsettings_tags %}
  77. {% get_settings %}
  78. {{ settings.app_label.SocialMediaSettings.instagram }}
  79. If there is no ``request`` available in the template at all, you can use the settings for the default Wagtail site instead:
  80. .. code-block:: html+django
  81. {% load wagtailsettings_tags %}
  82. {% get_settings use_default_site=True %}
  83. {{ settings.app_label.SocialMediaSettings.instagram }}
  84. .. 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.