Django项目外如何访问目录文件? [英] How to access directory file outside django project?

查看:618
本文介绍了Django项目外如何访问目录文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Django项目在RHEL 7 OS上运行.该项目在路径/root/project中.项目托管在httpd服务器上.现在,IAM尝试访问目录以外的文件,例如/root/data/info/test.txt

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

我应该如何在views.py中访问此路径,以便可以读取和写入项目目录之外的文件?我试图在sys.path中添加路径,但是没有用.读取和写入权限也授予该文件.

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.

推荐答案

将以下几行添加到您的settings.py

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

这篇关于Django项目外如何访问目录文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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