Django项目运行runserver报错:TypeError: Invalid path type: list
Django启动报错:TypeError: Invalid path type: list 排查方案
问题描述
学习《Django for Beginners》教程时,执行python manage.py runserver启动项目失败,抛出TypeError: Invalid path type: list错误。尝试移动文件夹、重启项目、核对教程步骤均未解决问题。
错误信息
(.venv) andrewstribling@Andrews-MBP pages % python manage.py runserver Watching for file changes with StatReloader Performing system checks... Traceback (most recent call last): File "/Users/andrewstribling/Desktop/code/pages/manage.py", line 22, in <module> main() File "/Users/andrewstribling/Desktop/code/pages/manage.py", line 18, in main execute_from_command_line(sys.argv) File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line utility.execute() File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/core/management/__init__.py", line 436, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/core/management/base.py", line 412, in run_from_argv self.execute(*args, **cmd_options) File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/core/management/commands/runserver.py", line 74, in execute super().execute(*args, **options) File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/core/management/base.py", line 458, in execute output = self.handle(*args, **options) File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/core/management/commands/runserver.py", line 111, in handle self.run(**options) File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/core/management/commands/runserver.py", line 118, in run autoreload.run_with_reloader(self.inner_run, **options) File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/utils/autoreload.py", line 671, in run_with_reloader start_django(reloader, main_func, *args, **kwargs) File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/utils/autoreload.py", line 660, in start_django reloader.run(django_main_thread) File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/utils/autoreload.py", line 343, in run autoreload_started.send(sender=self) File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/dispatch/dispatcher.py", line 176, in send return [ File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/dispatch/dispatcher.py", line 177, in <listcomp> (receiver, receiver(signal=self, sender=sender, **named)) File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/template/autoreload.py", line 43, in watch_for_template_changes for directory in get_template_directories(): File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/template/autoreload.py", line 20, in get_template_directories items.update(cwd / to_path(dir) for dir in backend.engine.dirs if dir) File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/template/autoreload.py", line 20, in <genexpr> items.update(cwd / to_path(dir) for dir in backend.engine.dirs if dir) File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/utils/_os.py", line 61, in to_path raise TypeError("Invalid path type: %s" % type(value).__name__) TypeError: Invalid path type: list
相关代码片段
django_project/urls.py
from django.contrib import admin from django.urls import path, include urlpatterns = [ path("admin/", admin.site.urls), path("", include("pages.urls")), ]
pages/urls.py
from django.urls import path from .views import HomePageView urlpatterns = [ path("", HomePageView().as_view(), name="home"), ]
settings.py
""" Django settings for django_project project. Generated by 'django-admin startproject' using Django 4.2.1. For more information on this file, see https://docs.djangoproject.com/en/4.2/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.2/ref/settings/ """ from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = "django-insecure-gh%z)%nhp0m3t%mo&2%k!7*^q^#rkt%7_exly%=%6(0_ae=ss0" # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", "pages", ] MIDDLEWARE = [ "django.middleware.security.SecurityMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", ] ROOT_URLCONF = "django_project.urls" TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", "DIRS": [[BASE_DIR / "templates"]], "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", ], }, }, ] WSGI_APPLICATION = "django_project.wsgi.application" # Database # https://docs.djangoproject.com/en/4.2/ref/settings/#databases DATABASES = { "default": { "ENGINE": "django.db.backends.sqlite3", "NAME": BASE_DIR / "db.sqlite3", } } # Password validation # https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", }, { "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", }, { "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", }, { "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", }, ] # Internationalization # https://docs.djangoproject.com/en/4.2/topics/i18n/ LANGUAGE_CODE = "en-us" TIME_ZONE = "UTC" USE_I18N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/4.2/howto/static-files/ STATIC_URL = "static/" # Default primary key field type # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
views.py
from django.shortcuts import render from django.views.generic import TemplateView # Create your views here. class HomePageView(TemplateView): template_name = "home.html"
解决方法
1. 修复模板路径配置(核心问题)
错误根源在settings.py的TEMPLATES配置中:DIRS字段被错误设置为嵌套列表,Django期望接收的是单个路径对象或字符串组成的列表,而非列表嵌套列表。
修改TEMPLATES中的DIRS:
"TEMPLATES": [ { "BACKEND": "django.template.backends.django.DjangoTemplates", "DIRS": [BASE_DIR / "templates"], # 去掉外层的额外列表包裹 "APP_DIRS": True, # 其他配置保持不变 }, ]
2. 修复URL配置的小问题(避免后续报错)
pages/urls.py中调用as_view()时错误实例化了类,as_view()是类方法,直接通过类名调用即可:
urlpatterns = [ path("", HomePageView.as_view(), name="home"), # 去掉括号,不要实例化HomePageView ]
修改完成后,重新执行python manage.py runserver即可正常启动项目。
内容的提问来源于stack exchange,提问作者user17598401
相关产品推荐
相关产品推荐

