wagtail/bakerydemo

Add Site Setting to the demo

lb- opened this issue · 3 comments

lb- commented

Site Settings are a key part of many Wagtail sites https://docs.wagtail.org/en/stable/reference/contrib/settings.html

It would be good to have an example in this demo site of a Site setting, ideally one that is translatable.

An example use case is some footer content that uses BaseSiteSetting (that can be translated).

Additionally, it would be great to have something that uses the BaseGenericSetting such as a logo.

See also #175

Imshk commented

I would to like to help on this issue , please provide me more information so I can solve it .

Hi, I have made a rough sketch of the steps needed to complete this task. Points taken from https://docs.wagtail.org/en/stable/reference/contrib/settings.html. If its valid can I do a PR and add these changes?

We create a new app within the bakerydemo project using python manage.py startapp mysettings
Add it to the INSTALLED_APPS setting -

INSTALLED_APPS = [
 ...
    'mysettings',
 ...
]

Now, create a models.py file in the mysettings app directory and import the BaseSiteSettings model from wagtail.contrib.settings.models

from wagtail.contrib.settings.models import BaseSiteSettings

class MySiteSettings(BaseSiteSettings):
    footer_text = models.CharField(
        verbose_name='Footer text',
        max_length=255,
        null=True,
        blank=True,
    )

    panels = BaseSiteSettings.panels + [
        FieldPanel('footer_text'),
    ]

    class Meta:
        verbose_name = 'Site settings'
        verbose_name_plural = 'Site settings'

Here, I've made MySiteSettings model that extends the BaseSiteSettings model and adds a footer_text field. It also have new panels attribute that includes the footer_text field in the admin interface.

Now, to use the new Site Settings, register it with Wagtail wagtail_hooks.py file

from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
from .models import MySiteSettings

class MySiteSettingsAdmin(ModelAdmin):
    model = MySiteSettings
    menu_label = 'Site settings'

modeladmin_register(MySiteSettingsAdmin)

I have extended the ModelAdmin class and set the model attribute to the new setting

Finally run the make migration codes as usual

For site-wide settings, change this in settings.py, so the new settings model is used - WAGTAIL_SITE_SETTINGS_MODEL = 'mysettings.MySiteSettings'

For using BaseGenericSetting as logo, we create a new model that inherits from BaseGenericSetting to represent the logo, and then register it with Wagtail in your app's wagtail_hooks.py file.

lb- commented

Hey @Hanoon02 thanks for this, I realised that the bakery demo site already has a footer model. It's a snippet.

Maybe the site setting could be a simple title suffix (that can be translated), one single char field.
This could then be used here https://github.com/wagtail/bakerydemo/blob/main/bakerydemo/templates/base.html#L15

A second field this model could be a colour field and then this sets the theme colour meta tag. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta/name/theme-color

Also, I don't think we would need a new app for this, in the base app is fine. Another thing that will need to be in the PR is the fixtures data.