【问题标题】:TypeError: expected str, bytes or os.PathLike object, not tuple in DjangoTypeError:预期的 str、bytes 或 os.PathLike 对象,而不是 Django 中的元组
【发布时间】:2020-09-20 18:32:21
【问题描述】:

我收到一个错误TypeError: expected str, bytes or os.PathLike object, not tuple 多次尝试从互联网上使用不同的方法,但没有任何效果。谁能帮我解决这个问题。

这是我在 settings.py 文件中的模板部分:

from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATE_DIRS = (os.path.join(BASE_DIR / 'templates'),
                 )
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIRS,],
        '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 django-templates


    【解决方案1】:

    这行好像是问题

    TEMPLATE_DIRS = (os.path.join(BASE_DIR / 'templates'),)
    

    DIRS 正在寻找一个列表,而不是元组列表,因此您可以使用

    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    

    或者如果你想保持自己的方式:

    TEMPLATE_DIRS = [os.path.join(BASE_DIR / 'templates'),]
    

    然后将DIRS设置为

    'DIRS': TEMPLATE_DIRS,
    

    【讨论】:

    • 然后我得到 预期的 str、bytes 或 os.PathLike 对象,而不是列表
    【解决方案2】:
    TEMPLATE_DIRS = (os.path.join(BASE_DIR / 'templates'),
    

    我认为正确的说法是:

    TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),
    

    然后将其作为DIRS 的值放入TEMPLATES 列表中

    另一种方法是简单地将(os.path.join(BASE_DIR, 'templates'), 放在TEMPLATES 列表中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-17
      • 2021-12-19
      • 1970-01-01
      • 2019-11-15
      • 2018-11-08
      • 2018-11-11
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多