【问题标题】:How to build a path in my templates file for Django如何在我的模板文件中为 Django 构建路径
【发布时间】:2021-07-15 18:00:17
【问题描述】:

现在在我的项目的 Django 设置文件中,我正在尝试构建一个路径,以便我可以显示我的模板/项目/index.html 文件。我可以寻求帮助解决这个问题吗?

    # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

在我的 TEMPLATES 字典中,这是 DIRS

`TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'projects/templates'),
        ],`

【问题讨论】:

  • 我注意到您在问题中说templates/projects,但在代码中说projects/templates——除此之外,还有什么没有按预期工作?有任何错误或意外行为吗?
  • projects/templates/projects/index.html 是我目前的设置方式。我收到 NameError 的名称错误:未定义名称“os”。
  • 这可以通过在文件顶部的某处添加import os 来解决。您需要对您的问题提供更详细的解释才能成为适合 SO 的问题,请参阅How do I ask a good question?

标签: python django django-templates


【解决方案1】:

我通过将 Templates.Dirs 更新为:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR / 'templates',
            'projects/templates',
        ],

【讨论】:

    猜你喜欢
    • 2016-12-10
    • 2018-02-04
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2014-05-22
    相关资源
    最近更新 更多