Django全局base.html模板

9
我是Django新手。我使用的是Django 1.8.6和Python 2.7。我正在尝试使用一个base.html模板,该模板可以在整个站点中全局使用,其中每个应用程序都可以访问它。这是我的测试网站的当前结构:
twms polls migrations static templates project migrations static templates project index.html tmws static templates tmws base.html
这是project/templates/project/index.html的代码。
{% extends 'tmws/base.html' %}
{% block content %}
    <h1>Projects</h1>
    <ul>
    {% for project in project_list %}
        <li><a href="{% url 'project:detail' project.id %}">{{ project.name }}</a></li>
    {% endfor %}

    </ul>
    end of list
{% endblock %}

我收到的错误是:

TemplateDoesNotExist at /project/

tmws/base.html

如何从我的任意应用程序中访问tmws/tmws/templates/tmws/base.html?

非常感谢您提供任何帮助!

如果需要任何其他信息,请告诉我。

编辑

以下是我的设置.py中的模板设置:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),  # If i leave both or just comment one one out I still get the same error
            'tmws.tmws.templates'
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

你的TEMPLATES设置中有什么内容? - Daniel Roseman
4个回答

18

我认为您可能在模板目录配置方面遇到问题。请在您项目的settings.py文件中检查是否有类似以下配置:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'your template dir name')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

其中'your template dir name'应该是tmws。这样做会使得Django在尝试扩展HTML模板时搜索该目录。你可以添加任意多个目录。

我认为现在Django一定正在以下位置搜索模板:

twms/project/templates/project

也许如果您将base.html文件放在那里,Django将能够找到它。

另一个建议是创建一个通用的templates目录,并将您的base.html文件放在那里,因为您希望它在整个站点中使用。这只是我对模板排序的喜好,但不管怎样都应该可以工作。

编辑:

在settings.py中添加以下内容解决了该问题。阅读评论以获取更多信息:

MASTER_BASE_DIR = os.path.dirname(__file__)




TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(MASTER_BASE_DIR, 'templates'),]
            ,
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]

1
不客气。你可以尝试添加:os.path.join(BASE_DIR, 'twms/templates')os.path.join(BASE_DIR, 'twms.templates')。我不确定是否可以在那里使用点符号表示法。 - Pablo Estrada
谢谢你的所有帮助,但我仍然遇到相同的错误。由于某种原因,我无法退出项目目录。我不确定这是否与BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file)))有关。我尝试了许多方法来操作它,但当我得到一个“此页面不可用”错误时,我还是无法解决问题。 - user908759
1
通过在settings.py中添加以下变量MASTER_BASE_DIR = os.path.dirname(__file__),然后将以下内容添加到Temaplates = [ ... 'DIRS':[os.path.join(MASTER_BASE_DIR, 'templates'),]],我已经让它正常工作了。请将此添加到您的答案中,我会标记它为正确。 - user908759
因为我包含了一个基础文件,所以我卡在这里很长时间,谢谢。 - shubhendu

0

您需要将所有模板文件夹添加到settings.py中的TEMPLATES设置中。然后,Django会将它们视为在同一个文件夹中。看起来您没有全部添加。您希望您的TEMPLATES看起来像这样:

TEMPLATES = [
    {
        'DIRS': [
            'twms.tmws.templates',  # Since this is not in an app
        ],
        'APP_DIRS': True,  # Automatically include the templates from INSTALLED_APPS
    },
]

0

你可以按照以下方式设置要搜索模板的目录:

        'DIRS': [os.path.join(MASTER_BASE_DIR, 'templates'),]

应该改为:

        'DIRS': [os.path.join(MASTER_BASE_DIR, 'tmws/templates'),]

为什么?因为BASE_DIR定义如下:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
#       -> os.path.dirname(os.path.dirname(/your/path/tmws/tmws/settings.py))
#       -> /your/path/tmws/

因此,BASE_DIR指向您的顶级文件夹,如果您将其与“templates”连接起来,则变为:/your/path/tmws/templates,但这个路径不存在。然而,在DIRS列表中更改了一行后,它将变成/your/path/tmws/tmws/templates,这才是正确的路径。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接