【问题标题】:TypeError: argument of type 'WindowsPath' is not iterable - in django python [duplicate]TypeError:'WindowsPath'类型的参数不可迭代-在django python中[重复]
【发布时间】:2021-01-07 07:03:49
【问题描述】:

每当我在终端中运行服务器或执行任何命令时,都会在终端中显示此错误。服务器正在运行,网页运行正常,但是当我退出服务器或运行任何命令(如 python manage.py migrate)时,会显示此错误。

   `Watching for file changes with StatReloader
    Performing system checks...
    
    System check identified no issues (0 silenced).
    September 21, 2020 - 12:42:24
    Django version 3.0, using settings 'djangoblog.settings'
    Starting development server at http://127.0.0.1:8000/
    Quit the server with CTRL-BREAK.

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:\Python37\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Python37\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python37\lib\site-packages\django\core\management\base.py", line 341, in run_from_argv
    connections.close_all()
  File "C:\Python37\lib\site-packages\django\db\utils.py", line 230, in close_all
    connection.close()
  File "C:\Python37\lib\site-packages\django\utils\asyncio.py", line 24, in inner
    return func(*args, **kwargs)
  File "C:\Python37\lib\site-packages\django\db\backends\sqlite3\base.py", line 261, in close
    if not self.is_in_memory_db():
  File "C:\Python37\lib\site-packages\django\db\backends\sqlite3\base.py", line 380, in is_in_memory_db
    return self.creation.is_in_memory_db(self.settings_dict['NAME'])
  File "C:\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
`

【问题讨论】:

  • 添加你的settings.py@Faiz P

标签: python django


【解决方案1】:

我通过更改 settings.py 文件中的 DATABASES 清除了这个问题:

改变

'NAME': BASE_DIR / 'db.sqlite3',

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

这行得通

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': str(os.path.join(BASE_DIR, "db.sqlite3"))
    }
}

【讨论】:

  • 请参阅my answer to a similar question 以了解该问题的可能解释。这个问题似乎在较新的 Django 版本中得到解决。
  • 记得import os。谢谢,这对我有帮助。
  • 效果很好
  • 更简单。就做'NAME': str(BASE_DIR + 'db.sqlite3'),
  • 我认为这个解决方案正在混合修复......一方面,使用os.path.join 似乎相对于Path(BASE_DIR)/'db.sqlite3' 提供的良好语法有点受挫。但是后来,str( ... ) 自己为我修复了错误。
猜你喜欢
  • 1970-01-01
  • 2023-03-14
  • 2021-02-14
  • 2019-07-23
  • 1970-01-01
  • 2012-04-26
  • 1970-01-01
  • 2022-11-08
  • 2021-10-28
相关资源
最近更新 更多