The solution I think of for this is to overwrite get_template_names
https://ccbv.co.uk/projects/Django/4.0/django.views.generic.base/TemplateView/#get_template_namesI quickly made a draft implementation (which I couldn't test)
from django.core.exceptions import ImproperlyConfigured
from django.views.generic import TemplateView
from app.models import UserTemplate
class PrependTemplatePathMixin:
def get_template_names(self):
"""
Return a list of template names to be used for the request. Must return
a list. May not be called if render_to_response() is overridden.
"""
if self.template_name is None:
raise ImproperlyConfigured(
"PrependTemplatePathMixin requires either a definition of "
"'template_name' or an implementation of 'get_template_names()'")
else:
user_template = UserTemplate.object.get(
user_id=
self.request.user.pk )
template_namespace = user_template.template_namespace.strip().replace('..', '').lstrip('/')
template_name = f'{template_namespace}/{self.template_name}'
return [template_name]
class MyTemplateView(PrependTemplatePathMixin, TemplateView):
template_name = 'my-template.html'
another option is to use or implement something similar to
https://github.com/jazzband/django-dbtemplates/