python - Quick and dirty way to access files external to Django?
问题描述
I'm working on a demo for a program that creates some files on its own directory. This demo will be shown to someone physically far, via VPN, so I made a simple django project just to receive an input, call some scripts and display the output - the generated file. However, I don't have permission to open the file to display it since it's on a directory outside of the django project (the result is a permission denied error).
I'm aware it's not good practice or even safe for a web server to have access to files outside of its directories, but since this will run in a closed environment for a short amount of time only, is there a workaround?
解决方案
Think of it this way - If web server can generate the files , it can display them also.
As for your answer - if you know the path of the file, use the python open built in method to open the file and render the result to a template.
data = open('file_path').read().decode('utf-8')
render(request, template, context={data:data})
推荐阅读
- ruby - 正则表达式匹配中英文标签
- python - 如何从电报机器人发送文件?
- amazon-web-services - 如何让第二个 NAT 备用,以防 AWS 上的同一公共子网上的主服务器发生故障
- reactjs - Gutenberg/React 将动态属性传递给过滤函数
- vba - 使用文件名中的 Outlook 子文件夹名称将邮件保存到硬盘驱动器
- firebase - 使用特定 UID 锁定写访问
- mysql - 转换用户输入 tp MYSQL TIME 文字
- c# - 存储过程在范围标识上返回 0
- python - 如何在本地 Python 项目的 Yocto 配方中安装 requirements.txt 的依赖项
- xslt - 如何在 xslt 或 xsl-fo 中按预期在 for 循环中进行排序(按字母顺序排列)?