将 Django 应用程序发布到 Azure 时出现服务器错误
Getting server error upon publishing django application to azure
我正在尝试使用 MSSQL 服务器将我的 Django 应用程序发布到 Azure。我正在使用 django-azure-pyodbc 和 pyodbc 进行连接,虽然它在本地工作,但每当我使用 mssql 发布时,我都会收到内部服务器错误,尽管发布成功。如果我使用预打包的 sqllite3 服务器发布,它就可以工作。我正在使用 Python 3.4、Django 1.8.4、pyodbc 3.0.10 和 django-pyodbc-azure 1.8.3.0 的虚拟环境。我的settings.py文件如下
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'db_name',
'USER': 'u_name',
'PASSWORD': 'p_word',
'HOST': 'host.database.windows.net',
'PORT': '',
}
}
我已将 Web 应用程序的传出 IP 地址添加到服务器,希望这是问题所在,但事实并非如此。检查诊断日志后,我发现了比 500 错误更详细的解释,但我不确定我是否理解它。
ModuleName FastCgiModule
Data1 FASTCGI_RESPONSE_ERROR
Data2 Error occurred while reading WSGI handler: Traceback (most
recent call last): File
"D:\home\site\wwwroot\env\lib\site-packages\sql_server\pyodbc\base.py",
line 14, in import pyodbc as Database ImportError: DLL load
failed: %1 is not a valid Win32 application. During handling of the
above exception, another exception occurred: Traceback (most recent
call last): File "D:\Python34\Scripts\wfastcgi.py", line 711, in main
env, handler = read_wsgi_handler(response.physical_path) File
"D:\Python34\Scripts\wfastcgi.py", line 568, in read_wsgi_handler
return env, get_wsgi_handler(handler_name) File
"D:\Python34\Scripts\wfastcgi.py", line 541, in get_wsgi_handler
handler = handler() File ".\ptvs_virtualenv_proxy.py", line 120, in
get_venv_handler handler =
get_wsgi_handler(os.getenv('WSGI_ALT_VIRTUALENV_HANDLER')) File
".\ptvs_virtualenv_proxy.py", line 78, in get_wsgi_handler handler =
handler() File
"D:\home\site\wwwroot\env\lib\site-packages\django\core\wsgi.py", line
14, in get_wsgi_application django.setup() File
"D:\home\site\wwwroot\env\lib\site-packages\django__init__.py", line
18, in setup apps.populate(settings.INSTALLED_APPS) File
"D:\home\site\wwwroot\env\lib\site-packages\django\apps\registry.py",
line 108, in populate app_config.import_models(all_models) File
"D:\home\site\wwwroot\env\lib\site-packages\django\apps\config.py",
line 198, in import_models self.models_module =
import_module(models_module_name) File
"D:\Python34\lib\importlib__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level) File
"", line 2254, in _gcd_import File
"", line 2237, in _find_and_load File
"", line 2226, in _find_and_load_unlocked
File "", line 1200, in _load_unlocked
File "", line 1129, in _exec File
"", line 1471, in exec_module File
"", line 321, in
_call_with_frames_removed File "D:\home\site\wwwroot\env\lib\site-packages\django\contrib\auth\models.py",
line 41, in class Permission(models.Model): File
"D:\home\site\wwwroot\env\lib\site-packages\django\db\models\base.py",
line 139, in new new_class.add_to_class('_meta', Options(meta,
**kwargs)) File "D:\home\site\wwwroot\env\lib\site-packages\django\db\models\base.py",
line 324, in add_to_class value.contribute_to_class(cls, name) File
"D:\home\site\wwwroot\env\lib\site-packages\django\db\models\options.py",
line 250, in contribute_to_class self.db_table =
truncate_name(self.db_table, connection.ops.max_name_length()) File
"D:\home\site\wwwroot\env\lib\site-packages\django\db__init__.py",
line 36, in getattr return getattr(connections[DEFAULT_DB_ALIAS],
item) File
"D:\home\site\wwwroot\env\lib\site-packages\django\db\utils.py", line
240, in getitem backend = load_backend(db['ENGINE']) File
"D:\home\site\wwwroot\env\lib\site-packages\django\db\utils.py", line
111, in load_backend return import_module('%s.base' % backend_name)
File "D:\Python34\lib\importlib__init__.py", line 109, in
import_module return _bootstrap._gcd_import(name[level:], package,
level) File "", line 2254, in _gcd_import
File "", line 2237, in _find_and_load
File "", line 2226, in
_find_and_load_unlocked File "", line 1200, in _load_unlocked File "", line
1129, in _exec File "", line 1471, in
exec_module File "", line 321, in
_call_with_frames_removed File "D:\home\site\wwwroot\env\lib\site-packages\sql_server\pyodbc\base.py",
line 16, in raise ImproperlyConfigured("Error loading pyodbc
module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error
loading pyodbc module: DLL load failed: %1 is not a valid Win32
application. StdOut: StdErr:
我真的不知道 Azure 是如何工作的,但我在混合使用 32 位和 64 位 driver/python 组合时遇到了这个错误。尝试使用 x64 位版本的 python 和驱动程序。
我无法重现您的问题,但我对您的软件包和版本进行了测试。我使用 Visual Studio 将 Django 应用程序发布到 Azure Web Apps,它运行良好。以下是我的步骤供您参考:
1,创建一个空的 Web App 并从本地 Git repo
设置部署
2、在Visual Studio中创建一个Django应用程序,添加python3.4的虚拟环境,然后安装python包,这里是requirements.txt
中的内容:
Django==1.8.4
pyodbc==3.0.10
django-pyodbc-azure==1.8.3.0
3、Azure上修改数据库设置为MSSQL,本地按F5测试
4、右击Django应用项目名,点击publish
部署
部署完成后,它会自动在默认浏览器中浏览您的应用程序。
问题是我的出版同事在他的环境中使用 64 位版本的 python。很简单,但我很感激你的帮助。
我正在尝试使用 MSSQL 服务器将我的 Django 应用程序发布到 Azure。我正在使用 django-azure-pyodbc 和 pyodbc 进行连接,虽然它在本地工作,但每当我使用 mssql 发布时,我都会收到内部服务器错误,尽管发布成功。如果我使用预打包的 sqllite3 服务器发布,它就可以工作。我正在使用 Python 3.4、Django 1.8.4、pyodbc 3.0.10 和 django-pyodbc-azure 1.8.3.0 的虚拟环境。我的settings.py文件如下
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'db_name',
'USER': 'u_name',
'PASSWORD': 'p_word',
'HOST': 'host.database.windows.net',
'PORT': '',
}
}
我已将 Web 应用程序的传出 IP 地址添加到服务器,希望这是问题所在,但事实并非如此。检查诊断日志后,我发现了比 500 错误更详细的解释,但我不确定我是否理解它。
ModuleName FastCgiModule
Data1 FASTCGI_RESPONSE_ERROR
Data2 Error occurred while reading WSGI handler: Traceback (most recent call last): File "D:\home\site\wwwroot\env\lib\site-packages\sql_server\pyodbc\base.py", line 14, in import pyodbc as Database ImportError: DLL load failed: %1 is not a valid Win32 application. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\Python34\Scripts\wfastcgi.py", line 711, in main env, handler = read_wsgi_handler(response.physical_path) File "D:\Python34\Scripts\wfastcgi.py", line 568, in read_wsgi_handler return env, get_wsgi_handler(handler_name) File "D:\Python34\Scripts\wfastcgi.py", line 541, in get_wsgi_handler handler = handler() File ".\ptvs_virtualenv_proxy.py", line 120, in get_venv_handler handler = get_wsgi_handler(os.getenv('WSGI_ALT_VIRTUALENV_HANDLER')) File ".\ptvs_virtualenv_proxy.py", line 78, in get_wsgi_handler handler = handler() File "D:\home\site\wwwroot\env\lib\site-packages\django\core\wsgi.py", line 14, in get_wsgi_application django.setup() File "D:\home\site\wwwroot\env\lib\site-packages\django__init__.py", line 18, in setup apps.populate(settings.INSTALLED_APPS) File "D:\home\site\wwwroot\env\lib\site-packages\django\apps\registry.py", line 108, in populate app_config.import_models(all_models) File "D:\home\site\wwwroot\env\lib\site-packages\django\apps\config.py", line 198, in import_models self.models_module = import_module(models_module_name) File "D:\Python34\lib\importlib__init__.py", line 109, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 2254, in _gcd_import File "", line 2237, in _find_and_load File "", line 2226, in _find_and_load_unlocked File "", line 1200, in _load_unlocked File "", line 1129, in _exec File "", line 1471, in exec_module File "", line 321, in _call_with_frames_removed File "D:\home\site\wwwroot\env\lib\site-packages\django\contrib\auth\models.py", line 41, in class Permission(models.Model): File "D:\home\site\wwwroot\env\lib\site-packages\django\db\models\base.py", line 139, in new new_class.add_to_class('_meta', Options(meta, **kwargs)) File "D:\home\site\wwwroot\env\lib\site-packages\django\db\models\base.py", line 324, in add_to_class value.contribute_to_class(cls, name) File "D:\home\site\wwwroot\env\lib\site-packages\django\db\models\options.py", line 250, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File "D:\home\site\wwwroot\env\lib\site-packages\django\db__init__.py", line 36, in getattr return getattr(connections[DEFAULT_DB_ALIAS], item) File "D:\home\site\wwwroot\env\lib\site-packages\django\db\utils.py", line 240, in getitem backend = load_backend(db['ENGINE']) File "D:\home\site\wwwroot\env\lib\site-packages\django\db\utils.py", line 111, in load_backend return import_module('%s.base' % backend_name) File "D:\Python34\lib\importlib__init__.py", line 109, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 2254, in _gcd_import File "", line 2237, in _find_and_load File "", line 2226, in _find_and_load_unlocked File "", line 1200, in _load_unlocked File "", line 1129, in _exec File "", line 1471, in exec_module File "", line 321, in _call_with_frames_removed File "D:\home\site\wwwroot\env\lib\site-packages\sql_server\pyodbc\base.py", line 16, in raise ImproperlyConfigured("Error loading pyodbc module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading pyodbc module: DLL load failed: %1 is not a valid Win32 application. StdOut: StdErr:
我真的不知道 Azure 是如何工作的,但我在混合使用 32 位和 64 位 driver/python 组合时遇到了这个错误。尝试使用 x64 位版本的 python 和驱动程序。
我无法重现您的问题,但我对您的软件包和版本进行了测试。我使用 Visual Studio 将 Django 应用程序发布到 Azure Web Apps,它运行良好。以下是我的步骤供您参考: 1,创建一个空的 Web App 并从本地 Git repo
设置部署2、在Visual Studio中创建一个Django应用程序,添加python3.4的虚拟环境,然后安装python包,这里是requirements.txt
中的内容:
Django==1.8.4
pyodbc==3.0.10
django-pyodbc-azure==1.8.3.0
3、Azure上修改数据库设置为MSSQL,本地按F5测试
4、右击Django应用项目名,点击publish
部署
部署完成后,它会自动在默认浏览器中浏览您的应用程序。
问题是我的出版同事在他的环境中使用 64 位版本的 python。很简单,但我很感激你的帮助。