Home > Software engineering >  List & tuple aren't working despite addressing `django.core.excptions.ImproperlyConfigured: The
List & tuple aren't working despite addressing `django.core.excptions.ImproperlyConfigured: The

Time:11-06

I've been hitting a snag with my Django app. Running Python manage.py runserver in my development environment, I get this error: django.core.excptions.ImproperlyConfigured. The TEMPLATE_DIRS setting must be a list or a tuple. After changing TEMPLATE_DIRS to a tuple in settings.py as this TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),) I got :

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

I got a similar response when I changed it to a list. Meanwhile, running Python manage.py runserver outside the development environment I got:

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

I should also add that my template files aren't loading. I get:

Using the URLconf defined in myProject.urls, Django tried these URL patterns, in this order:

accounts/

admin/

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

Here is the project tree:

|_______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',
            ], },  },]

I don't know how else to fix it.

CodePudding user response:

TEMPLATE_DIRS is already a list, so it makes no sense to wrap that again in the list, you should define the TEMPLATES setting as:

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