我为我的新项目创建了一个虚拟环境,安装了 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',
}
}
看起来 NAME 正在被转换为 pathlib.Path (WindowsPath) 对象而不是字符串,然后不能以与 os.path 期望字符串相同的方式在 Django 中使用字符串(不是 100% 确定,因为没有深入调查)
所以用字符串进行转换是合适的
'NAME': str(os.path.join(BASE_DIR, "db.sqlite3"))
确保您确实在 venv 中执行了命令(您应该看到
(venv)
)
如果你像@iklinac所说的那样,这应该可以解决你的问题:
'NAME': str(os.path.join(BASE_DIR, "db.sqlite3"))
此外,对于 Django>=3.1,路径模块包含在 os 模块的位置。因此,使用:
'NAME': str(BASE_DIR / 'db.sqlite3')
因此,DB sqlite3 设置在 settings.py 中将如下所示。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': str(BASE_DIR / 'db.sqlite3')
}
}
你可以尝试在这个地方加一个逗号 STATICFILES_DIRS = (BASE_DIR / '静态',) 如果末尾没有逗号,则会弹出此错误