在Django项目中获取typeError错误

2024-09-30 16:30:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我为我的新项目创建了一个虚拟环境,安装了django并启动了新项目。然而,每当我使用manage.py运行一行代码时,我都会遇到这个长错误

PS D:\My stuff\Website development\Isow website\isow> python manage.py makemigrations
No changes detected
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\rahma\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\rahma\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\rahma\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 336, in run_from_argv
    connections.close_all()
  File "C:\Users\rahma\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\utils.py", line 224, in close_all
    connection.close()
  File "C:\Users\rahma\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\sqlite3\base.py", line 248, in close
    if not self.is_in_memory_db():
  File "C:\Users\rahma\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\sqlite3\base.py", line 367, in is_in_memory_db
    return self.creation.is_in_memory_db(self.settings_dict['NAME'])
  File "C:\Users\rahma\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\sqlite3\creation.py", line 12, in is_in_memory_db
    return database_name == ':memory:' or 'mode=memory' in database_name
TypeError: argument of type 'WindowsPath' is not iterable

数据库条目:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

Tags: djangoinpydblibpackageslocalline
3条回答

此外,对于Django>=3.1中,包含path模块以代替操作系统模块。因此,使用:

'NAME': str(BASE_DIR / 'db.sqlite3')

因此,DB sqlite3设置在settings.py中看起来像这样

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': str(BASE_DIR / 'db.sqlite3')
    }
}

确保您确实在venv中执行了命令(您应该看到(venv)

如果你像@iklinac所说的那样,这将解决你的问题:

'NAME': str(os.path.join(BASE_DIR, "db.sqlite3"))

名称似乎正在转换为pathlib.Path(WindowsPath)对象,而不是字符串,因此无法在Django中使用该字符串,就像os.path所期望的字符串一样(不是100%确定,因为没有深入调查)

所以用线来浇铸是合适的

'NAME': str(os.path.join(BASE_DIR, "db.sqlite3"))

相关问题 更多 >