How to access directory file outside django project?

谁说胖子不能爱 提交于 2019-12-22 09:06:41

问题


I have my Django project running on RHEL 7 OS. The project is in path /root/project. And project is hosted on httpd server. Now iam trying to access a file out side the directory like /root/data/info/test.txt

How should I access this path in views.py so that I can read and write file which is outside the project directory ? I tried to add the path in sys.path but it didn't work. Read and write permission are also give to the file.


回答1:


Add the following lines to your settings.py

import os
..
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
FILES_DIR = os.path.abspath(os.path.join(BASE_DIR, '../data/info'))

Then you can use in your view

from django.conf import settings
import os
..
file_path = os.path.join(settings.FILES_DIR, 'test.txt')


来源:https://stackoverflow.com/questions/39228449/how-to-access-directory-file-outside-django-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!