【问题标题】:django on app engine does not render templates in sub directories应用引擎上的 django 不会在子目录中呈现模板
【发布时间】:2019-02-26 04:58:12
【问题描述】:

有人将此标记为重复 - 由于以下原因,它不是他们链接到的问题的重复。首先,我不会为每个 url 收到 404 错误。我得到了一些模板而不是其他模板,我在模板文件夹的顶层获取了所有模板,而不是子目录中的模板。它还给出了 500 个错误而不是 404 个错误,这是不同类别的错误。

我一直在尝试将 django(2.1) (python3) 应用程序放在 google 的灵活应用引擎上,但遇到了以下问题:

应用程序不会呈现模板文件夹子目录中的任何模板,它会给出 500 错误。我一直在通过 gcloud 控制台和管理界面查看日志的尾部,但没有看到任何有用的信息。

我的 app.yaml 是:

runtime: python
# api_version: 1

env: flex
entrypoint: gunicorn -b :$PORT MyApp.MyApp.wsgi
runtime_config:
    python_version: 3

manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

env_variables:
  SECRET_KEY: 'key-here'
  DEBUG: 'False'
  DB_HOST: '/cloudsql/instance:region:instance'
  DB_PORT: '5432'
  DB_NAME: 'instance'
  DB_USER: 'postgres'
  DB_PASSWORD: 'db-password'
  STATIC_URL: 'https://storage.googleapis.com/bucket-name/static/'

beta_settings:
  cloud_sql_instances: 'instance:region:instance'

我的模板文件夹设置: BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(文件)))

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'),
                 os.path.join(BASE_DIR, 'templates', 'subdir'),
                 os.path.join(BASE_DIR, 'templates', 'subdir', 'othersubdir'),

等等

我真的很难过。任何有关从何处获取更详细的错误日志或可能存在的问题的建议或反馈将不胜感激 - 谢谢!!!

【问题讨论】:

  • 是只在 App Engine 上失败,还是在本地也失败?
  • 仅在应用引擎上,在本地它与开发服务器和 gunicorn 都可以正常工作。
  • This 问题与您的问题相似。
  • 这不是同一个问题,但感谢您的链接。

标签: django python-3.x google-app-engine app-engine-flexible


【解决方案1】:

尝试更直接的方法,例如:

TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), 'templates'),)
TEMPLATE_DIRS += (os.path.join(os.path.dirname(__file__), 'templates/subdir'),)
TEMPLATE_DIRS += (os.path.join(os.path.dirname(__file__), 'templates/subdir/othersubdir'),)

os.path.dirname(__file__)settings.py 的位置,所以从那里一切都是相对的。

【讨论】:

  • 谢谢!我试过了,但没有运气。我想我要重新开始了:-(
【解决方案2】:

也许在重新开始之前,您可以在 Stackdriver 日志上看到一些更具体的错误,您可以使用过滤器按资源过滤日志,也许您将能够看到有关 GAE 应用程序错误的更多详细信息。

无论如何...如果您已经重新开始,也许您可​​以use this example project 作为参考,以查看所需的所有配置和实现

【讨论】:

    猜你喜欢
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 2021-09-05
    相关资源
    最近更新 更多