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

标签 python django django-templates

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

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

问题

我想在 events.html 中扩展 base.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',

)

最佳答案

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

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

您可以将 base.html 移至 project/templates,或通过以下方式引用它:

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

关于python - 如何从另一个模块扩展模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25701479/

相关文章:

javascript - 在 Flask 表单上生成 Markdown 预览

python - 使用Python从OpenDAP批量下载数据

python - 从 Django 中的表名中查找模型实例

Django,自动生成唯一的模型字段,如果不唯一则递归调用自动生成器

html - django 将 html href 和 src 替换为 {% static %} 标签

Django Includes - 它们是邪恶的吗?

django - 访问模板内的foreignKey

python - 拖放脚本行为不正常

python - 按值排序字典,然后按键

django - Azure 上 Django 的 GitHub : Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'