python - 如何在 Django 中检查与 mysql 的数据库连接

标签 python django django-settings

我该怎么做?

我想,我可以从数据库中读取一些东西,但是看起来太多了,有没有类似的东西?:

settings.DATABASES['default'].check_connection()

最佳答案

您需要做的就是启动一个应用程序,如果它没有连接,它将失败。您可以尝试的其他方法是在 shell 上尝试以下 -

from django.db import connections
from django.db.utils import OperationalError
db_conn = connections['default']
try:
    c = db_conn.cursor()
except OperationalError:
    connected = False
else:
    connected = True

关于python - 如何在 Django 中检查与 mysql 的数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32098797/

相关文章:

python - 我们如何删除 SAP BOM 中的虚拟层?

python - 使用 openpyxl 验证电子表格的结构

python - 将spark转换为pandas数据帧有异常: arrow is not supported when using file-based collect

Django:安全中间件使站点崩溃

django - 属性错误 : 'tuple' object has no attribute 'startswith'

python - 导入错误 : cannot import name 'Flask' from partially initialized module 'flask' (most likely due to a circular import)

python - 如何翻译动态表单选择

python - Django - 如何基于具有 OneToOneField 的用户模型查询用户个人资料

python - 在 Django 管理界面的字段中包含来自另一个模型的多对多

python - 为什么 Django 抛出错误 "DisallowedHost at/"?