0

I want to be able to have dynamic directories where templates will reside. Let me explain what I mean. I have an application system wich has such a file structure:

\proj
    __init__.py
    settings.py
    urls.py
    ...
\system
    __init__.py
    models.py
    views.py
    urls.py
    \modules
        \module_1
            __init__.py
            models.py
            views.py
            urls.py
            \templates     ## Attention
                one.html
                two.html
        \module_2
            __init__.py
            modules.py
            \templates    ##
                three.html
                four.html
        ...
        \module_N
            ...

As you can see there is a modules folder, which contains "atomic" modules, atomic in a sense that they have all necessary files, including templates in one place. So that module_1 has a template folder with its tempaltes, module_2 has a templates folder and all other modules have their own templates folder. What I want is to be able to refer to these templates folders in my settings.py file, so that when I upload a brand new module to modules folder, I would not have to change this settings.py file. So my question is, how can I dynamically build TEMPLATE_DIRS variable:

TEMPLATE_DIRS = (
    ## how to implement this???
)

EDIT

I'm considering another approach. First, to convert my modules to dinamic applications like this:

MODULES_DIR = 'system/modules'

for item in os.listdir(MODULES_DIR):
    if os.path.isdir(os.path.join(MODULES_DIR, item)):
        app_name = 'system.modules.%s' % item
        INSTALLED_APPS += (app_name, )

And then to do something to make Django look for templates in all applications' folders. But I'm not sure whether it will work and how can I complete this task.

Community
  • 1
  • 1
Jacobian
  • 10,122
  • 29
  • 128
  • 221

1 Answers1

2

This is simply app-specific templates, which is already catered for by the APP_DIRS flag in the template settings dict. There shouldn't be any other configuration needed.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • And will my approach of building INSTALLED_APPS dynamically work? – Jacobian Sep 20 '15 at 14:59
  • I suspect that will depend on when that code runs; you will probably run into difficulties with the startup order. I must say though I can't see why you're doing this; if you're adding code to your project, it's trivial to add that code's app name to INSTALLED_APPS. – Daniel Roseman Sep 20 '15 at 15:11
  • And I'm not sure that I understand documentation right. Can you, please, correct me, if I am mistaken? As far as I can gather, I should have all my modules listed in INSTALLED_APPS (statically, or dynamically if it is possible). And then just have APP_DIRS = True. And all this should be done in settings.py file. – Jacobian Sep 20 '15 at 15:12
  • " I must say though I can't see why you're doing this; " The idea is to enable users to upload new modules from a remote server without having to change any code. – Jacobian Sep 20 '15 at 15:15