django生产服务器:根路径 [英] django production server: root path

查看:136
本文介绍了django生产服务器:根路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Django应用程序.在开发服务器上,一切正常. 在生产服务器上(使用apache),没有任何工作.

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

1/我在/处出现错误 TemplateDoesNotExist.

1/ I have the error TemplateDoesNotExist at /.

在我的settings.py文件中:

In my settings.py file:

SITE_ROOT = os.path.abspath(os.path.dirname(__name__)).这是项目的根路径.

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
)

这是模板路径.

2/如果我用项目的绝对路径更改SITE_ROOT:

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

SITE_ROOT="/var/www/europolix"

模板似乎可以识别,但是我还有另一个错误: 没有名为getEurlexIdsFunctions的模块 这是代码:

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 

我认为问题再次出自相对路径. Apache似乎在"var/www/"而不是"var/www/europolix/"中搜索导入".我说的对吗?

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?

这是我的Apache配置:

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?

非常感谢.

推荐答案

嗯,有几件事.与settings.py一起使用时,最好将所有路径声明为绝对路径.我在您的代码中看到您有这一行

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__))

用于网站的根目录,但是我认为如果使用__file__全局变量而不是__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__))

我在生产服务器中有一个django应用程序,关于wsgi我需要添加到httpd.conf中的所有内容是load_module指令和虚拟主机中的这一行

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

django.wsgi脚本的别名指定为根.

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

然后在django.wsgi脚本中,我有以下代码:

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.

希望有帮助.

这篇关于django生产服务器:根路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆