【问题标题】:Fail to launch Django project on localhost无法在本地主机上启动 Django 项目
【发布时间】:2020-04-20 18:18:09
【问题描述】:

我尝试从 GitHub https://github.com/konradgalczynski07/vinciu打开这个项目

我用Pycharm打开了整个编码文件。

当我使用 localhost 运行 Web 应用程序时,它会失败并显示以下错误。我应该如何配置这个Django 项目并在 localhost 上启动?

>Watching for file changes with StatReloader
>     Performing system checks...
>     
>     System check identified no issues (0 silenced).
>     Exception in thread django-main-thread:
>     Traceback (most recent call last):
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\base\base.py",
> line 220, in ensure_connection
>         self.connect()
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\asyncio.py",
> line 26, in inner
>         return func(*args, **kwargs)
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\base\base.py",
> line 197, in connect
>         self.connection = self.get_new_connection(conn_params)
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\asyncio.py",
> line 26, in inner
>         return func(*args, **kwargs)
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\postgresql\base.py",
> line 185, in get_new_connection
>         connection = Database.connect(**conn_params)
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\psycopg2\__init__.py",
> line 126, in connect
>         conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
>     psycopg2.OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
>     Is the server running on host "localhost" (::1) and accepting
>     TCP/IP connections on port 5432?
>     could not connect to server: Connection refused (0x0000274D/10061)
>     Is the server running on host "localhost" (127.0.0.1) and accepting
>     TCP/IP connections on port 5432?
>     
>     
>     The above exception was the direct cause of the following exception:
>     
>     Traceback (most recent call last):
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\threading.py",
> line 932, in _bootstrap_inner
>         self.run()
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\threading.py",
> line 870, in run
>         self._target(*self._args, **self._kwargs)
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\autoreload.py",
> line 53, in wrapper
>         fn(*args, **kwargs)
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\commands\runserver.py",
> line 120, in inner_run
>         self.check_migrations()
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py",
> line 458, in check_migrations
>         executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\executor.py",
> line 18, in __init__
>         self.loader = MigrationLoader(self.connection)
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\loader.py",
> line 49, in __init__
>         self.build_graph()
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\loader.py",
> line 212, in build_graph
>         self.applied_migrations = recorder.applied_migrations()
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\recorder.py",
> line 76, in applied_migrations
>         if self.has_table():
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\recorder.py",
> line 56, in has_table
>         return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\asyncio.py",
> line 26, in inner
>         return func(*args, **kwargs)
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\base\base.py",
> line 260, in cursor
>         return self._cursor()
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\base\base.py",
> line 236, in _cursor
>         self.ensure_connection()
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\asyncio.py",
> line 26, in inner
>         return func(*args, **kwargs)
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\base\base.py",
> line 220, in ensure_connection
>         self.connect()
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\utils.py",
> line 90, in __exit__
>         raise dj_exc_value.with_traceback(traceback) from exc_value
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\base\base.py",
> line 220, in ensure_connection
>         self.connect()
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\asyncio.py",
> line 26, in inner
>         return func(*args, **kwargs)
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\base\base.py",
> line 197, in connect
>         self.connection = self.get_new_connection(conn_params)
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\asyncio.py",
> line 26, in inner
>         return func(*args, **kwargs)
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\postgresql\base.py",
> line 185, in get_new_connection
>         connection = Database.connect(**conn_params)
>       File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\psycopg2\__init__.py",
> line 126, in connect
>         conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
>     django.db.utils.OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
>     Is the server running on host "localhost" (::1) and accepting
>     TCP/IP connections on port 5432?
>     could not connect to server: Connection refused (0x0000274D/10061)
>     Is the server running on host "localhost" (127.0.0.1) and accepting
>     TCP/IP connections on port 5432?

【问题讨论】:

    标签: django django-models django-rest-framework django-templates pycharm


    【解决方案1】:

    错误似乎源于数据库配置中的问题。查看该存储库中的 settings.py 文件后,我注意到当前配置正在尝试使用本地主机上的 postgres 服务器。但是,您似乎没有使用这些设置运行 postgres 服务器,因此 django 无法连接到该数据库并引发错误。我可以推荐使用此处文档中的 sqlite 选项:https://docs.djangoproject.com/en/3.0/ref/settings/#databases

    确认数据库设置与文档中的设置相同后,使用 manage.py 重新运行迁移并创建 sqlite 数据库中所需的所有表。

    使用此 sqlite 数据库将有利于开发和测试,但是一旦您准备好进入生产环境,您将需要切换回 postgresql 或其他类型的基于服务器的数据库。

    【讨论】:

    • 改回sqlite Db设置后,我应该在控制台上执行python manage.py makemigrations&python manage.py migrate吗?谢谢
    • 迁移很可能已经完成,但您肯定需要至少运行 python manage.py migrate
    • 如果我用 sqlite db 托管服务器有什么缺点?
    猜你喜欢
    • 2023-01-08
    • 2017-01-18
    • 2015-08-05
    • 2019-04-20
    • 2016-10-21
    • 2019-09-05
    • 1970-01-01
    • 2016-09-30
    • 2020-03-11
    相关资源
    最近更新 更多