Changing Template Location in Wagtail

Viewed 125

I'm trying to direct wagtail to use a template in a centralised location at the top of the project tree. So for example:

Project
|_ HomePage
|_ Search
|_ Mypage
|_ templates

My project re-uses templates and I'd like to give UX/UI Developers access to a single folder rather than multiple sub-folders in multiple pages.

I've tried in Mypage/models.py

class AdvisorSummaryPages(Page):
    
    intro = models.CharField(max_length=250, blank=True, null=True)
    
    content_panels = Page.content_panels + [
     FieldPanel('intro'),
     InlinePanel('carousell', heading="Carousell Images")   
    ]
    
    template = "advisors.html"

With the following template setting in Project/Project/settings/base.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
        ],

With no luck. I can't seem to find any solution on SO or through the documentation or Google that might work. There is a solution presented here using separate model admins but that doesn't work for me. How might I specify the location of the template differently to a subdirectory of templates in the MyPage App?

Thanks

1 Answers

Below is how I organize templates and static assets. I have a themes folder that is located in the main project folder with named theme subfolders within the themes folder. I then have the following in settings/base.py:

PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)

THEMES_URL = '/themes/'
THEME_PATH = os.path.join(BASE_DIR, THEMES_URL.replace('/', ''), 'name_of_theme')

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            THEME_PATH,
            ...other directories...,
        ],
... other TEMPLATES-RELATED CODE
     }
]

STATICFILES_DIRS = [
    os.path.join(THEME_PATH, 'static'),
]


STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

The name_of_theme folder in the THEME_PATH definition contains all of the templates and static files for the theme. In order for the static files to be collected correctly, the folder structure for the css, js, etc. files within each theme folder needs to be: /themes/name_of_theme/static/name_of_theme/js (or css, etc.)/filename.js (or filename.css, etc.)

The /name_of_theme/static/name_of_theme/ namespacing is necessary for collectstatic to be able to collect the files correctly (see Staticfile namespacing here for more info). When including a reference to a static file in a template, you then do:

{% static 'name_of_theme/js/filename.js' %}

The STATICFILES_DIRS definition is only set up for one theme. You would need to change or add to that if you're using more than one theme.

Some time ago I also came across this Wagtail package: Wagtail Themes. It looks very interesting, but I'm not sure if it provides for handling static files organized within named theme folders as I describe above.

Related