django production server: root path

2019-07-21 23:10发布

I am developing a django application. On the development server, everything works just fine. On the production server (using apache), nothing is working.

1/ I have the error TemplateDoesNotExist at /.

In my settings.py file:

SITE_ROOT = os.path.abspath(os.path.dirname(__name__)). This is the project root path.

templateDir = os.path.join(SITE_ROOT, 'templates/')
TEMPLATE_DIRS = (
    templateDir
)

This is the templates path.

2/ If I change SITE_ROOT with the absolute path of the project:

SITE_ROOT="/var/www/europolix"

Templates seem to be recognize but I have another mistake: No module named getEurlexIdsFunctions Here is the code:

import sys
sys.path.append('import')
import getEurlexIdsFunctions as eurlexIds 

I think that once again the problem comes from a relative path. Apache seems to search 'import' in "var/www/" and not in "var/www/europolix/". Am I right?

Here is my apache configuration:

WSGIScriptAlias /europolix /var/www/europolix/europolix/wsgi.py
WSGIPythonPath /var/www/europolix/
<Directory /var/www/europolix/>
   <Files wsgi.py>
       Order deny,allow
       Allow from all
   </Files>
</Directory>

Is it a problem of root path not recognized, or is there another problem?

Many thanks.

2条回答
家丑人穷心不美
2楼-- · 2019-07-21 23:45

in django project in file wsgi.py i add this 3 lines

import sys
DJANGO_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)),'..')
sys.path.append(DJANGO_PATH)

查看更多
再贱就再见
3楼-- · 2019-07-21 23:50

Well, a couple of things. When working with settings.py is better to declare all the paths as absolute paths. I see in your code that you have this line

SITE_ROOT = os.path.abspath(os.path.dirname(__name__))

for site's root but I think is better if you use __file__ global variable instead of __name__. Like this:

SITE_ROOT = os.path.abspath(os.path.dirname(__file__))

I have a django app in production server and all I had to add to my httpd.conf about wsgi was the load_module directive and this line inside the virtual host

WSGIScriptAlias / C:/Users/ike/Documents/Work/Sincronoz/code/apache/django.wsgi

specifying the alias to the django.wsgi script as the root.

Then in django.wsgi script I have this code:

import os, sys
sys.path.append(r'<full site root to where is settings.py>')
os.environ['DJANGO_SETTINGS_MODULE'] = 'my_project_module.settings'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

I think you're better off working with absolute paths and when you got it all working then try to accommodate it for your needs with maybe relative path if you have to.

Hope it helps.

查看更多
登录 后发表回答