【问题标题】:Django not loading 'extends' template in productionDjango 未在生产中加载“扩展”模板
【发布时间】:2023-03-03 19:43:01
【问题描述】:

我是 Python/Django 的新手,但我正在尝试在 AWS Elastic Beanstalk 中发布一个应用程序,但它没有找到 一些 模板。

当我请求http://www.example.com/path 时,它会抛出一个错误:TemplateDoesNotExist at /path

它说:

Error during template rendering

In template /opt/python/bundle/5/app/src/app_name/templates/app_name/tmpl.djhtml, error at line 1

1   {% extends 'app_name/base.djhtml ' %}
2   ...

因此,它会加载主模板,但不会加载使用extends 指定的模板。更糟糕的是,它说:

Django tried loading these templates, in this order:

Using engine django:
...
django.template.loaders.app_directories.Loader:
/opt/python/bundle/5/app/src/app_name/templates/app_name/base.djhtml (Source does not exist)

如果我通过 SSH 连接到服务器,该文件确实存在!它也适用于我的 Windows 机器。

如果我在该目录中执行ls -hal,则两个文件的权限相同:

-rw-r--r-- 1 wsgi root  512 Oct 21 17:18 tmpl.djhtml
-rw-r--r-- 1 wsgi root 3.0K Oct 21 17:18 base.djhtml

模板设置如下:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates').replace('\\','/'),],
        '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',
            ],
        },
    },
]

有什么想法吗?谢谢。

【问题讨论】:

    标签: python django amazon-web-services django-templates amazon-elastic-beanstalk


    【解决方案1】:

    找到了!经过一天尝试不同的事情,但在我发布问题后仅 5 分钟。

    问题出在这里:

    {% extends 'app_name/base.djhtml ' %}
    

    文件名后面有一个额外的空格。正确的,显然是:

    {% extends 'app_name/base.djhtml' %}
    

    这在 Windows 中似乎无关紧要,但在 Linux 中却很重要!

    【讨论】:

      猜你喜欢
      • 2011-08-02
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 2015-11-11
      • 1970-01-01
      相关资源
      最近更新 更多