Dynamically add to TEMPLATE_DIRS at runtime a Django project

For the Django project I'm working on, I need to allow the user to specify the path used in TEMPLATE_DIRS. This is done to implement selectable "topics." For instance:

TEMPLATE_DIRS = (
    os.path.join(WEBSITE_ROOT, 'templates', THEME_NAME).replace('\\', '/'),
    os.path.join(WEBSITE_ROOT, 'templates', 'default').replace('\\', '/'),
)

But the THEME_NAME variable must come from the database through the site administration.

Any ideas?

+3
source share
3 answers

Write a template loader , you can specify the theme directory.

+4
source

I did something similar, see here https://github.com/ASKBOT/askbot-devel/blob/master/askbot/skins/loaders.py

, . , , , url - , . , .js .

+1

Not sure if this is the same issue as my question switching themes, template and CSS file on django site

template subdirs can be specified as context var, which is used as a parameter to extend the template tag

0
source

Source: https://habr.com/ru/post/1775918/


All Articles