在Apache上部署Django时出错

时间:2009-03-23 15:52:46

标签: python windows django apache mod-python

我有一个小的Django网站,我试图在Apache 2.2 HTTP-Server上运行。 使用“python manage.py runserver”运行应用程序正常。

Django版本:1.0.2最终版 Python:2.5
操作系统:Windows 2000

我没有通过documentation中描述的步骤,经过一些摆弄,在我的httpd.conf中提出了以下内容。

<Location "/therap/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE settings
PythonOption django.root /therap
PythonDebug On
PythonPath "['D:/therap/therap'] + sys.path"
</Location>

MaxRequestsPerChild 1

D:\治疗\治疗在我的manage.py所在的地方。

当我尝试在浏览器中打开时,我看到Django使用的样式出错(而不是白色背景上的黑色快递。)

ImportError at /

No module named therap.urls

Request Method: GET
Request URL:    http://****:8080/
Exception Type: ImportError
Exception Value:   

No module named therap.urls

Exception Location: C:\python25\lib\site-packages\django\core\urlresolvers.py in _get_urlconf_module, line 200
Python Executable:  C:\Programme\Apache Software Foundation\Apache2.2\bin\httpd.exe
Python Version:     2.5.1
Python Path:        ['D:/therap/therap', 'C:\\WINNT\\system32\\python25.zip', 'C:\\Python25\\Lib', 'C:\\Python25\\DLLs', 'C:\\Python25\\Lib\\lib-tk', 'C:\\Programme\\Apache Software Foundation\\Apache2.2', 'C:\\Programme\\Apache Software Foundation\\Apache2.2\\bin', 'C:\\Python25', 'C:\\Python25\\lib\\site-packages', 'C:\\Python25\\lib\\site-packages\\pyserial-2.2', 'C:\\Python25\\lib\\site-packages\\win32', 'C:\\Python25\\lib\\site-packages\\win32\\lib', 'C:\\Python25\\lib\\site-packages\\Pythonwin', 'C:\\Python25\\lib\\site-packages\\wx-2.8-msw-unicode']
Server time:        Mo, 23 Mär 2009 16:27:03 +0100

D:\ therap \ therapy中有一个urls.py. 然而,D:\ therap \ therap \ main中没有一个我的大部分代码都是。

然后我尝试使用父文件夹

<Location "/therap/">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE therap.settings
    PythonOption django.root /therap
    PythonDebug On
    PythonPath "['D:/therap'] + sys.path"

</Location>

MaxRequestsPerChild 1

这给了我一个不同的错误:

MOD_PYTHON ERROR

ProcessId:      2424
Interpreter:    '***'

ServerName:     '****'
DocumentRoot:   'C:/Programme/Apache Software Foundation/Apache2.2/htdocs'

URI:            '/therap/'
Location:       '/therap/'
Directory:      None
Filename:       'C:/Programme/Apache Software Foundation/Apache2.2/htdocs/therap'
PathInfo:       '/'

Phase:          'PythonHandler'
Handler:        'django.core.handlers.modpython'

Traceback (most recent call last):

  File "C:\Python25\Lib\site-packages\mod_python\importer.py", line 1537, in HandlerDispatch
    default=default_handler, arg=req, silent=hlist.silent)

  File "C:\Python25\Lib\site-packages\mod_python\importer.py", line 1229, in _process_target
    result = _execute_target(config, req, object, arg)

  File "C:\Python25\Lib\site-packages\mod_python\importer.py", line 1128, in _execute_target
    result = object(arg)

  File "C:\Python25\lib\site-packages\django\core\handlers\modpython.py", line 228, in handler
    return ModPythonHandler()(req)

  File "C:\Python25\lib\site-packages\django\core\handlers\modpython.py", line 201, in __call__
    response = self.get_response(request)

  File "C:\python25\Lib\site-packages\django\core\handlers\base.py", line 67, in get_response
    response = middleware_method(request)

  File "C:\python25\Lib\site-packages\django\middleware\locale.py", line 17, in process_request
    translation.activate(language)

  File "C:\python25\Lib\site-packages\django\utils\translation\__init__.py", line 73, in activate
    return real_activate(language)

  File "C:\python25\Lib\site-packages\django\utils\translation\trans_real.py", line 209, in activate
    _active[currentThread()] = translation(language)

  File "C:\python25\Lib\site-packages\django\utils\translation\trans_real.py", line 198, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)

  File "C:\python25\Lib\site-packages\django\utils\translation\trans_real.py", line 183, in _fetch
    app = __import__(appname, {}, {}, [])

ImportError: No module named main

我确实使用国际化模块,但我不明白为什么它会导致问题。

“main”是唯一的Django应用程序的名称(包含视图,模型,表单等)。完整的路径是D:\ therap \ therap \ main。

我把__init__.py - 文件从主文件夹到d:\ therap。

现在我不知道还能做些什么。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

问题是您正在导入您的应用程序(“main”),就像它直接存在于Python路径上一样,而您的URLconf(“therap.urls”)就像它存在于Python上的“治疗”模块中一样路径。这只有在“D:/治疗”和“D:/治疗/治疗”两者都在Python路径上都是有效的(runserver会自动为你做“让事情变得容易”;虽然它最终只会延迟混乱直到部署时间)。您可以通过在Apache配置中使用以下行来模拟runserver的行为:

PythonPath "['D:/therap', 'D:/therap/therap'] + sys.path"

标准化引用可能更有意义,因此Python路径只需要包含一个或另一个。通常的方式(至少我经常看到引用的方式)是将“D:\ therap”放在Python路径上,并将您的应用程序限定为“therap.main”而不仅仅是“main”。就个人而言,我采取相反的方法,它工作得很好:在你的Python路径上放“D:\ therap \ therap”并将ROOT_URLCONF设置为“urls”而不是“therap.urls”。这样做的好处是,如果将来你想让你的“主”应用程序可重用并将其移出特定项目,你对它的引用不会与项目名称“治疗”相关联(尽管有一个名为“app”的应用程序)主要的“它听起来并不像你在重用应用程序方面的想法。”