S
S
softshape2015-10-13 12:17:45
Django
softshape, 2015-10-13 12:17:45

How to override template.dirs in Django?

The situation is this - one server serves several domains. Each of them has its own base.html and directories look like this -
templates/domain1/base.html
templates/domain2/base.html
templates/domain3/base.html
We made a middleware that looks at the domain and overrides templates.dirs, adds to it the desired directory. And we see in debugging that templates.dirs takes the correct value. However, the template loader spits on it! It loaded the templates.dirs value at the start of Django, so it continues to work with it. As a result, all domains use the base.html of some one domain (the one that was loaded first).
How to wean him from this? .... django.template.loaders.cached.Loader is disabled, if that.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
U
un1t, 2015-10-13
@un1t

I suspect that you need to override Loader and write it in the template settings
from django.template.loaders.app_directories import Loader

N
Nikita Shultais, 2015-10-13
@shultais

Redefining settings is bad practice :)
You can go a little different way:
1. Change the path to the template in views
2. Dynamically generate template names in views and context_processros, and then pass them as variables to templates for use in include and extends.
{% extends domain_base_template %}

S
softshape, 2015-10-15
@softshape

In general, they thought and thought and returned to the decision from which they initially wanted to leave. This is the standard Jungian "running multiple copies, each with its own settings.py". She solves our problem like a cannon on sparrows, but she solves it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question