python manage.py runserver: TypeError: argument 1 must be str not WindowsPath

时间:2021-02-14 08:34:24

标签: python django

我正在跟进 django 教程,我刚刚使用 pip install django=2.1 安装了 django,并且成功安装,然后使用 django-admin startproject App . 创建了一个项目,之后我尝试使用 {{1} 运行服务器} 并且我得到了一个 python manage.py runserver

TypeError: argument 1 must be str not WindowsPath

这里是 settings.py:

Performing system checks...

System check identified no issues (0 silenced).
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x02AC2D20>
Traceback (most recent call last):
  File "C:\Users\ASUS\PycharmProjects\WebDev\venv\lib\site-packages\django\utils\autoreload.py", line 225, in
 wrapper
    fn(*args, **kwargs)
  File "C:\Users\ASUS\PycharmProjects\WebDev\venv\lib\site-packages\django\core\management\commands\runserver
.py", line 120, in inner_run
    self.check_migrations()
  File "C:\Users\ASUS\PycharmProjects\WebDev\venv\lib\site-packages\django\core\management\base.py", line 442
, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "C:\Users\ASUS\PycharmProjects\WebDev\venv\lib\site-packages\django\db\migrations\executor.py", line 1
8, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Users\ASUS\PycharmProjects\WebDev\venv\lib\site-packages\django\db\migrations\loader.py", line 49,
 in __init__
    self.build_graph()
  File "C:\Users\ASUS\PycharmProjects\WebDev\venv\lib\site-packages\django\db\migrations\loader.py", line 209
, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "C:\Users\ASUS\PycharmProjects\WebDev\venv\lib\site-packages\django\db\migrations\recorder.py", line 6
1, in applied_migrations
    if self.has_table():
  File "C:\Users\ASUS\PycharmProjects\WebDev\venv\lib\site-packages\django\db\migrations\recorder.py", line 4
4, in has_table
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor(
))
  File "C:\Users\ASUS\PycharmProjects\WebDev\venv\lib\site-packages\django\db\backends\base\base.py", line 25
5, in cursor
    return self._cursor()
  File "C:\Users\ASUS\PycharmProjects\WebDev\venv\lib\site-packages\django\db\backends\base\base.py", line 23
2, in _cursor
    self.ensure_connection()
  File "C:\Users\ASUS\PycharmProjects\WebDev\venv\lib\site-packages\django\db\backends\base\base.py", line 21
6, in ensure_connection
    self.connect()
  File "C:\Users\ASUS\PycharmProjects\WebDev\venv\lib\site-packages\django\db\backends\base\base.py", line 19
4, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\ASUS\PycharmProjects\WebDev\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line
 159, in get_new_connection
    conn = Database.connect(**conn_params)
TypeError: argument 1 must be str, not WindowsPath

我是不是遗漏了什么。请帮忙! 此外,stackoverflow 不允许我添加更多信息,因为它说我的问题主要是代码。

1 个答案:

答案 0 :(得分:7)

问题来了

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

BASE_DIR / 'db.sqlite3' 返回一个 Pathlib 对象(WindowsPath 如果您使用的是 Windows,否则为 PosixPath),而 NAME 实际上期望一个字符串。所以只需将 BASE_DIR / 'db.sqlite3' 更改为 str(BASE_DIR / 'db.sqlite3')