13

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 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?

jdphenix
  • 15,022
  • 3
  • 41
  • 74
Fred Collins
  • 5,004
  • 14
  • 62
  • 111

6 Answers6

36

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()
Community
  • 1
  • 1
ayaz
  • 10,406
  • 6
  • 33
  • 48
  • Getting a `Caught VariableDoesNotExist while rendering: Failed lookup for key [0] in u'[]'` when trying to insert (or append) `../../` & `../` to the path – Pierre de LESPINAY Oct 17 '11 at 06:46
  • 1
    Wow you are my hero, been fighting with this for like 3 hours now, didn't think to move stuff further up the path list. I wish I could upvote more! – Kevin DiTraglia Apr 02 '14 at 22:52
  • 1
    You saved my life ! Damn! i've pased two days searching for this solution. Thanks man! – Chiheb Nexus Jul 09 '17 at 13:08
  • 1
    Thanks a lot for this. I had been hitting a wall cuz of this for the last 4-5 hours!! – Aish Feb 21 '18 at 08:38
25

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.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 3
    This was my problem too. Why the hell isn't this mentioned in the docs though!!!!!!!!!! I thought you just had to specify the DJANGO_SETTINGS_MODULE and django would then take care of the rest. – Sam May 13 '11 at 12:04
  • Note that the path should actually read "/srv/www/site.com", **since that's what matches what the asker has put in their question**. – Ignacio Vazquez-Abrams Mar 20 '14 at 05:40
11

Seconding ayaz' answer. It's important that the paths you're specifying be at the beginning of the search path, which means you need to use 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')
MacGyverQue
  • 181
  • 2
  • 4
5

try following this tutorial - 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. your wsgi.py should look like

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()
Shashank Singla
  • 1,797
  • 17
  • 13
2

What corrected this error for me --

Changing settings.py from:

ROOT_URLCONF = 'urls'

To this:

ROOT_URLCONF = 'myproject.urls'

Ken
  • 23
  • 4
  • This was a thorn in my side also. This was the cause. I had to search (egrep) my entire site to find out where it was trying to get the urls file from. – Furbeenator Apr 26 '12 at 15:53
1

where you have djproj.urls maybe try just urls

darren
  • 18,845
  • 17
  • 60
  • 79