[英]Django + mod_wsgi + apache: ImportError at / No module named djproj.urls
I'm trying to deploy my Django application on my linode server with apache and mod_wsgi.我正在尝试使用 apache 和 mod_wsgi 在我的 linode 服务器上部署我的 Django 应用程序。
file: /srv/www/example.com/djproj/django.wsgi
import os
import sys
sys.path.append('/srv/www/example.com/djproj')
os.environ['PYTHON_EGG_CACHE'] = '/srv/www/example.com/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
file: /etc/apache2/sites-available/example.com
/etc/apache2/sites-available/example.com
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example.com/public_html
WSGIScriptAlias / /srv/www/example.com/djproj/django.wsgi
<Directory "/srv/www/example.com/djproj">
Order allow,deny
Allow from all
</Directory>
ErrorLog /srv/www/example.com/logs/error.log
CustomLog /srv/www/example.com/logs/access.log combined
When I visit / of my site I get this error :当我访问 / 我的网站时,我收到此错误:
Environment:
Request Method: GET
Request URL: http://example.com/
Django Version: 1.3
Python Version: 2.6.6
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/local/lib/python2.6/dist-packages/Django-1.3- py2.6.egg/django/core/handlers/base.py" in get_response
101. request.path_info)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py 2.6.egg/django/core/urlresolvers.py" in resolve
250. for pattern in self.url_patterns:
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/core/urlresolvers.py" in _get_url_patterns
279. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/core/urlresolvers.py" in _get_urlconf_module
274. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/utils/importlib.py" in import_module
35. __import__(name)
Exception Type: ImportError at /
Exception Value: No module named djproj.urls
I can't get it to work.我无法让它工作。 Ideas?想法?
I second Ignacio Vazquez-Abrams's answer .我支持 Ignacio Vazquez-Abrams 的回答。 You must add the path to your project directory as well as the path to its parent directory to sys.path
.您必须将项目目录的路径及其父目录的路径添加到sys.path
。 Here is an example of the WSGI script file I use.这是我使用的 WSGI 脚本文件的示例。 I keep the file inside the project directory.我将文件保存在项目目录中。
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../")))
os.environ["DJANGO_SETTINGS_MODULE"] = "PROJECT_NAME.settings"
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()
Either change all your module/package entries and imports to exclude the project name, or put /srv/www/site.com
in sys.path
as well.更改所有模块/包条目和导入以排除项目名称,或者将/srv/www/site.com
也放入sys.path
。
Seconding ayaz' answer.附议ayaz的回答。 It's important that the paths you're specifying be at the beginning of the search path, which means you need to use insert..重要的是您指定的路径位于搜索路径的开头,这意味着您需要使用 insert..
Here's mine.这是我的。 When I was doing an 'append' I was getting intermittent issues.当我进行“追加”时,我遇到了间歇性问题。 This made it rock solid.这使它坚如磐石。 Hope this helps.希望这可以帮助。
sys.path.insert(0, '/srv/www/Appname')
sys.path.insert(1, '/srv/www')
try following this tutorial - http://singlas.in/5-step-tutorial-for-using-django-with-apache-and-mod_wsgi/尝试遵循本教程 - http://singlas.in/5-step-tutorial-for-using-django-with-apache-and-mod_wsgi/
Make sure to add both Django directory and Django Project Directory to system path.确保将 Django 目录和 Django 项目目录都添加到系统路径。 your wsgi.py should look like你的 wsgi.py 应该看起来像
import os,sys
#comments
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_project.settings")
sys.path.append('/path/to/your/django/directory/django_project')
sys.path.append('/path/to/your/django/directory')
#comments
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
What corrected this error for me --是什么为我纠正了这个错误——
Changing settings.py from:从以下位置更改 settings.py:
ROOT_URLCONF = 'urls' ROOT_URLCONF = '网址'
To this:对此:
ROOT_URLCONF = 'myproject.urls' ROOT_URLCONF = 'myproject.urls'
where you have djproj.urls
maybe try just urls
你有djproj.urls
的地方可能只尝试urls
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.