开发者

Relative path in settings.py in django

开发者 https://www.devze.com 2023-04-09 19:04 出处:网络
for various settings(MEDIA_ROOT,TEMPLATE_DIRS) in setting.py it is instructed to give absolute path.I have configured apache with mod_wsgi.I have a wsgi script in the folder named apache that redirect

for various settings(MEDIA_ROOT,TEMPLATE_DIRS) in setting.py it is instructed to give absolute path.I have configured apache with mod_wsgi.I have a wsgi script in the folder named apache that redirects to settings.py.

import os
import sys

path = 'D:/Projects/Dan'

if path not in sys.path:
  sys.path.append(path )

os.environ['DJANGO_SETTINGS_MODULE'] = 'Django.settings'

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

(don't misunderstand - Django is the name of my project.)

Okay my question is - now that we have imported the path to my project to system path in the wsgi script, isn't it more feasible that I give a rel开发者_运维百科ative path in settings.py,since that would make deploying easier.If I am wrong please tell me the standard procedure so that I can set all the path in one file other than 3 files(Apache - httpd.conf, mod_wsgi - django.wsgi, django - settings.py).


Use:

import os
this_directory = os.path.dirname(__file__)

and then:

absolute_directory = os.path.join(this_directory, 'relative.txt')

BTW, calling your project 'Django' is ill advised given that Django package itself is 'django'. You shouldn't rely on differences in case not causing confusion. In short, Python packages/module names should ever differ just by case. Your site project directory is treated as a Python package and thus the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号