作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发 Django Web 应用程序,对于这个应用程序,我希望有一个在线数据库。碰巧我的教员授予使用某些 Microsoft Azure 服务的权限,包括 SQL 数据库。现在,我想将 Azure SQL 数据库连接到我的 Django 项目,但是我无法让它工作。我可以通过 PyCharm 的数据源 View 连接到数据库,但是在 settings.py 中使用相同的参数会产生此错误:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x03846270>
Traceback (most recent call last):
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
autoreload.raise_last_exception()
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\autoreload.py", line 250, in raise_last_exception
six.reraise(*_exception)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\registry.py", line 108, in populate
app_config.import_models()
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\base_user.py", line 52, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\base.py", line 124, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\base.py", line 330, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\options.py", line 214, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\utils.py", line 212, in __getitem__
conn = backend.DatabaseWrapper(db, alias)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sqlserver_ado\base.py", line 184, in __init__
super(DatabaseWrapper, self).__init__(*args, **kwargs)
File "C:\Users\kairy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\base\base.py", line 96, in __init__
self.client = self.client_class(self)
TypeError: 'NoneType' object is not callable
settings.py 中的数据库对象如下所示(敏感信息被编辑掉):
DATABASES = {
'default': {
"ENGINE": "sqlserver_ado",
'NAME': [databasename],
"HOST": "tcp:[resourcegroup].database.windows.net",
"PORT": "1433",
"USER": [username@resourcegroup],
"PASSWORD": [password]
}
}
这个配置有什么问题吗?
最佳答案
根据我的经验,如果您想从 Django webapp 连接 Azure SQL 数据库,最佳实践的简单方法是遵循 django-pyodbc-azure
README 安装 python 包 pyodbc
和 django-pyodbc-azure
并正确配置 Django 的 settings.py
文件,如下所示。
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'mydb',
'USER': 'user@myserver',
'PASSWORD': 'password',
'HOST': 'myserver.database.windows.net',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
},
},
}
关于python - 如何将 microsoft azure SQL 服务器连接到 django webapp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43717906/
我是一名优秀的程序员,十分优秀!