Django Project: Trying to understand why my Django project is showing the error Understanding TypeError Invalid path type list:

I am trying to understand why my Django project is saying that it cannot understand my paths. According the instructions of the book django for beginners I need to run the python manage.py runserver command to load the website but it stops me.

It shows the following when I get the error:

Error Message

(.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/andrewstri
(.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.shortcuts import render 

from django.views.generic import TemplateView

# Create your views here.

class HomePageView(TemplateView):
    template_name = "home.html"

settings.py

` ```
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "pages",
]

views.py

from django.shortcuts import render 

from django.views.generic import TemplateView

# Create your views here.

class HomePageView(TemplateView):
    template_name = "home.html" 

What steps have I tried and what am I expecting?

I have tried moving my folders around, I have tried restarting and running the same commands again. I have tried searching on stackoverflow for articles related to the subject. I have tried checking the python documentation.

Could my file directory have something to do with this?

I am at a loss because my path is in a and thats what url patterns are supposed to be in.
Any ideas?

There’s an indication that the problem is in the setup of the template directories.

From the traceback:

The first thing I would look at would be the TEMPLATES setting in the settings.py.

Per the instructions from the Django for Beginners books I was told to make my directory like this:

TEMPLATES = [
    {
        ...
        "DIRS": [BASE_DIR / "templates"],  # new
        ...
}, 
]

my settings.py file shows:

# 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",
            ],
        },
    },
]

Error Mesasge

(.venv) andrewstribling@Andrews-MBP pages % python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/threading.py", line 973, in _bootstrap_inner
    self.run()
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/core/management/commands/runserver.py", line 133, in inner_run
    self.check(display_num_errors=True)
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/core/management/base.py", line 485, in check
    all_issues = checks.run_checks(
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/core/checks/urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/core/checks/urls.py", line 24, in check_resolver
    return check_method()
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/urls/resolvers.py", line 494, in check
    for pattern in self.url_patterns:
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/utils/functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/urls/resolvers.py", line 715, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/utils/functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/urls/resolvers.py", line 708, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/andrewstribling/Desktop/code/pages/django_project/urls.py", line 22, in <module>
    path("", include("pages.urls")), 
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/urls/conf.py", line 38, in include
    urlconf_module = import_module(urlconf_module)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/andrewstribling/Desktop/code/pages/pages/urls.py", line 5, in <module>
    path("", HomePageView().as_view(), name="home"),
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/utils/decorators.py", line 9, in __get__
    raise AttributeError(
AttributeError: This method is available only on the class, not on instances.
pwd

looking at the error message it is talking about decorators in django/utils. I didn’t install any decorators.

The question I start asking my wlf is where in this error code do I have my problem?

I do see the error message above it in path(“”, HomePageView().as_view(), name=“home”),

I will check the solution code

This:

is not the same as this:

1 Like

Also incountered raise AttributeError(AttributeError: This method is available only on the class not the instance)

reading the error message location I saw that I had HomePageView in my url patterns have a () when should only have been on the as-view() method.

YAY! by reading the error lines in the program the other error code pointed to where I had made another mistake and I solved it!

I am learning to crawl, soon I will be able to walk, and than SPRINT!

thanks Ken.