【问题标题】:List & tuple aren't working despite addressing `django.core.excptions.ImproperlyConfigured: The TEMPLATE_DIRS setting must be a list or a tuple` error尽管解决了 `django.core.excptions.ImproperlyConfigured: The TEMPLATE_DIRS setting must be a list or a tuple` 错误,但列表和元组不起作用
【发布时间】:2021-11-05 21:10:24
【问题描述】:

我的 Django 应用遇到了麻烦。在我的开发环境中运行 Python manage.py runserver,我得到这个错误: django.core.excptions.ImproperlyConfigured. The TEMPLATE_DIRS setting must be a list or a tuple。在将 TEMPLATE_DIRS 更改为 settings.py 中的元组后,TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),) 我得到了:

TypeError('Invalid path type: %s' % type(value).__name__)TypeError: Invalid path type: tuple.

当我将其更改为列表时,我得到了类似的响应。 同时,在我得到的开发环境之外运行 Python manage.py runserver:

expected str, bytes or os.PathLike object, not tuple on both times I changed TEMPLATE_DIRS to a list and tuple.

我还应该补充一点,我的模板文件没有加载。我明白了:

使用 myProject.urls 中定义的 URLconf,Django 尝试了这些 URL 模式,按以下顺序:

accounts/

admin/

The current path, index/, didn't match any of these

这是项目树:

|_______myApp
|______ |______pycache__
|   |______migrations
|   |________init__
|   |______admin
|   |______apps
|   |______models
|   |______tests
|   |______urls
|   |______views
|______myProject
|   |________init__
|   |______asgi
|   |______settings
|   |______urls
|   |______wsgi
|______env
|______static
|______templates
    |______myApp
        |______index.html
        |______signup.html
manage.py

myProject/urls.py

from django.contrib import admin
from django.urls import include, path 

urlpatterns = [
    path('accounts/', include('accounts.urls')),
    path('admin/', admin.site.urls), 
]

myApp/urls.py

from django.urls import path
from . import views
urlpatterns = [
    path('', views.index, name='index'),
    path('signup/', views.signup, name='signup'),
    ]

views.py

from django.urls import path
from . import views
urlpatterns = [
    path('', views.index, name='index'),
    path('signup/', views.signup, name='signup'),   
]

settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
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',
                'django.template.context_processors.media',
            ], },  },]

我不知道如何解决它。

【问题讨论】:

  • 使用'DIRS': TEMPLATE_DIRS,

标签: django


【解决方案1】:

TEMPLATE_DIRS 已经是一个列表,因此在列表中再次包装它是没有意义的,您应该将TEMPLATES 设置定义为:

TEMPLATES = [
    {
        # …,
        'DIRS': TEMPLATE_DIRS,
        # …
    }
  ]

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 2021-10-15
    • 2017-12-03
    • 1970-01-01
    相关资源
    最近更新 更多