Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

python - 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.

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 [email protected]
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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I second Ignacio Vazquez-Abrams's answer. You must add the path to your project directory as well as the path to its parent directory to sys.path. Here is an example of the WSGI script file I use. 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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...