问题描述
我有一个小型的Django网站,我试图运行在Apache 2.2 HTTP服务器上。应用程序使用python manage.py runserver运行正常。
Django版本:1.0.2最终
Python: 2.5
操作系统:Windows 2000
我没有通过,并且在一些虚幻之后,在我的httpd.conf中出现了以下内容。
<位置/ therap />
SetHandler python程序
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE设置
PythonOption django.root / therap
PythonDebug On
PythonPath[ 'D:/ therapy / therap'] + sys.path
< / Location>
MaxRequestsPerChild 1
D:\therap\therap beeing the place我的manage.py是。
当我尝试在浏览器中打开时,我发现Django使用的风格出现错误(而不是在白色背景上的黑色快递。)
ImportError at /
没有名为therap.urls的模块
请求方法:GET
请求URL:http:// ****:8080 /
异常类型:ImportError
异常值:
无模块命名.urls
异常位置:C:\python25\lib\site-packages\django\core\urlresolvers.py在_get_urlconf_module中,行200
Python可执行文件:C :\Programme\Apache Software Foundation\Apache2.2\bin\httpd.exe
Python版本:2.5.1
Python路径:['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-包','C:\\Python25\\lib\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\Python25\\lib\\\\site-packages\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\lib','C:\\Python25\\lib\\\\site-packages\\Pythonwin','C:\\Python25\\lib\\ site-packages\\wx-2.8-msw-unicode']
服务器时间:Mo,23Mär2009 16:27:03 +0100
D:\therap中有一个urls.py \\therap。
然而,D:\therap\therap\main中没有我的大部分代码。
然后,我尝试使用父文件夹
<位置/ therap />
SetHandler python程序
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
口译:'***'
ServerName:'****'
DocumentRoot:'C:/ Program / Apache Software Foundation / Apache2.2 / htdocs'
URI:'/ therap /'
位置:'/ therap /'
目录:无
文件名:'C:/ Program / Apache Software Foundation / Apache2.2 / htdocs / therap'
PathInfo:'/'
阶段:'PythonHandler'
处理程序:'django.core.handlers.modpython'
追溯(最近的最后一次调用):
文件C:\Python25\Lib\site- packages\mod_python\importer.py,第1537行,HandlerDispatch
default = default_handler,arg = req,silent = hlist.silent)
文件C:\Python25\Lib\site-packages\mod_python\importer.py,行1229,在_process_target
result = _execute_target(config,req,object,arg)
文件C:\Python25\Lib\site-packages\mod_python\importer。 py,第1128行,在_execute_target
result = object(arg)
文件C:\Python25\lib\site- packages\django\core\handlers \modpython.py,第228行,处理程序
return ModPythonHandler()(req)
文件C:\Python25\lib\site-packages\django\\ \\ core \handlers\modpython.py,第201行,__call__
response = self.get_response(request)
文件C:\python25\Lib\site packages\django\core\handlers\base.py,第67行,get_response
response = middleware_method(request)
文件C:\python25\ lib \site-packages\django\middleware\locale.py,第17行,process_request
translation.activate(language)
文件C: \python25\Lib\site-packages\django\utils\translation\__init __。py,第73行,激活
return real_activate(language)
文件C:\python25\Lib\site-packages\django\utils\translation\trans_real.py,第209行,激活
_active [currentThread()] =翻译(语言)
文件C:\python25\Lib\site- packages\django\utils\translation\trans_real.py,第198行,翻译
default_translation = _fetch(settings.LANGUAGE_CODE)
文件C:\python25\Lib\site- packages\django\utils\translation\trans_real.py,第183行,_fetch
app = __import __(appname,{},{},[])
ImportError:没有名为main的模块
我使用国际化模块,但我不明白为什么会导致问题。
main 是唯一的Dj的名字ango应用程序(包含视图,模型,表单等)。完整的路径是D:\therap\therap\main。
我把 __ init __。py
文件从主文件夹到d:\therap。
现在我不知道还能做什么。任何想法?
问题是您正在导入您的应用程序(main),就好像它直接存在于Python路径和您的URLconf(therap.urls),就像它位于Python路径上的治疗模块中一样。只有D:/治疗和D:/治疗/治疗在Python路径上都是双重的(运行服务器为您自动使事情变得容易),只能延迟混淆直到部署时间)。您可以使用Apache配置中的以下行来模拟runserver的行为:
PythonPath['D:/ therap',' D:/ therapy / therap'] + sys.path
引用,所以你的Python路径只需要包括一个或另一个。通常的方法(至少我看到的更经常引用的方式)是将D:\therap放在Python路径上,并将您的应用限定为therap.main而不是main。就个人而言,我采取相反的方法,它的工作原理很好:将D:\therap\therap放在你的Python路径上,并将ROOT_URLCONF设置为urls而不是therap.urls。这样做的优点是,如果将来您希望使您的主要应用程序可重用并将其从特定项目中移出,则您对其的引用不会与项目名称治疗相关联(尽管应用程序名为主要这听起来不像您正在考虑可重用的应用程序)。
I have a small Django website which I am trying to run on an Apache 2.2 HTTP-Server.The application is running fine using "python manage.py runserver".
Django Version: 1.0.2 final
Python: 2.5
OS: Windows 2000
I wen't through the steps described in the documentation and after some fiddling, came out with the following in my 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:\therap\therap beeing the place where my manage.py is.
When I try to open in my browser, I see an error in the style used by Django (as opposed to black courier on white background.)
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
There is a urls.py in D:\therap\therap.However there is none in D:\therap\therap\main where most of my code is.
I then tried using the parent folder
<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
Which gave me a different error:
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
I do use the internationalization module, but I do not see why it causes a problem at this point.
"main" is the name of the only Django app (containing views, models, forms and such). The full path is D:\therap\therap\main.
I put __init__.py
-files everywhere from the main folder to d:\therap.
Now I don't know what else I could do. Any ideas?
The problem is that you are importing your app ("main") as if it lives directly on the Python path, and your URLconf ("therap.urls") as if it lives within a "therap" module on the Python path. This can only work if both "D:/therap" and "D:/therap/therap" are BOTH on the Python path (which runserver does for you automatically to "make things easy"; though it ends up just delaying the confusion until deployment time). You can emulate runserver's behavior by using the following line in your Apache config:
PythonPath "['D:/therap', 'D:/therap/therap'] + sys.path"
It probably makes more sense to standardize your references so your Python path only need include one or the other. The usual way (at least the way I see referenced more often) would be to put "D:\therap" on the Python path and qualify your app as "therap.main" instead of just "main". Personally, I take the opposite approach and it works just fine: put "D:\therap\therap" on your Python path and set ROOT_URLCONF to "urls" instead of "therap.urls". The advantage of this is that if in future you want to make your "main" app reusable and move it out of the particular project, your references to it aren't tied to the project name "therap" (though with an app named "main" it doesn't sound like you're thinking in terms of reusable apps anyway).
这篇关于在Apache上部署Django时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!