python - 如何调用应用程序文件夹外的模板?

标签 python django python-3.x

如何调用header.html?这是我的文件夹结构。我在/处遇到 TemplateSyntaxError

- Project Name
    - app
      - home
         - templates      //will contain all the views
             - home.html
         - views..py
      - templates         //will contain all the scripts,styles
         - header.html 

Here is my code: Home/templates/home.html

{% extends 'header.html' %}
{% block content %}
    <body>
    </body>
{% endblock content %}

更新:这是我的Settings.py:

APP_ROOT = os.path.join(BASE_DIR,'apps')

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            APP_ROOT + '/home/templates',
            APP_ROOT + '/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'
            ],
        },
    },
]

最佳答案

根据您项目的目录树,您不需要任何进一步的配置。只需更换

{% extends '../../templates/header.html' %}

{% extends 'header.html' %}

然后改变

APP_ROOT + 'home.templates',

APP_ROOT + 'home/templates',

关于python - 如何调用应用程序文件夹外的模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54290628/

相关文章:

python - Python 3 中自定义类的迭代器

python - 如何使用opencv在python中计算不对称形状区域

django - Django 的 S3 子域

python - 如何在 Python 3 中使用面向对象编程读取文本文件

Django 1.9 编译错误

mysql - 在 Django 项目之外使用 Django ORM,“MySQL 已经消失”

python - 我如何按数组的行和/或列对平均值进行排序?

python - 添加到字典中键的值

python - 无法导入 'keystoneauth1' ...尽管已安装?

python - Pythons random.randint 在统计上是随机的吗?