如何从另一个模块扩展模板?

时间:2014-09-06 14:53:19

标签: python django django-templates

我有一个具有以下结构的项目:

project    
    project
        static/
        templates
            project
                base.html
        __init__.py
        .....
    events
        static/
        templates
            events
                events.html
        __init__.py
        models.py
        .....

问题

我想在base.html

中扩展events.html

我尝试过:

{% extends "project:project/base.html" %}

但是得到以下错误

Exception Type:     TemplateDoesNotExist
Exception Value:    project:project/base.html

还试过:

{% extends "base.html" %}

但是返回了相同的错误

Exception Type:     TemplateDoesNotExist
Exception Value:    base.html

INSTALLED_APPS我有:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'events',
    'project',
)

TEMPLATE_DIRSBASE_DIR

TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

TEMPLATE_LOADERS我有:

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',

)

1 个答案:

答案 0 :(得分:1)

您的TEMPLATE_DIR设置为project/templates,而不是project/templates/project

由于base.html中没有project/templates,Django会返回错误。

您可以将base.html移至project/templates,也可以写下来参考:

{% extends "project/base.html" %}
相关问题