anon
anon

Reputation:

Django path to templates for apps

Let's assume I have the following project:

myproject/
    myproject/
        __init__.py
        settings.py
        urls.py
        wsgi.py
        templates/
            base.html
    app1/
        __init__.py
        admin.py
        models.py
        urls.py
        views.py
        templates/
            base.html
            index.html

I want to have a base template, which all other apps will use, in myproject/template/base.html.
Then, I want to have the app templates in app/templates.

One option would be writting in myproject/settings.py:

TEMPLATE_DIRS = (
    "/dir/to/myproject/myproject/templates",
    "/dir/to/myproject/app1/templates",
)

But, is this the best way to do it?

Upvotes: 0

Views: 77

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599610

Django supports this automatically, via the app_directories loader that is installed by default. See the documentation.

Upvotes: 1

Sudipta
Sudipta

Reputation: 4971

Maybe you can do the following:

#settings.py
import os
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
    os.path.join(PROJECT_PATH, 'templates'),
    os.path.join(PROJECT_PATH, 'app1/templates'),
)

This will save you from editing each and every absolute path in setting file in case you happen to move your project.

Upvotes: 0

Related Questions