首页 > 解决方案 > 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?

标签: pythondjangopermissions

解决方案


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

推荐阅读