我在我的一个Django模型中有一个ImageField。这些图像中的每一个都具有可以访问它们的用户(或用户组);没有其他用户应该能够看到它们。
ImageField将图像文件存储在媒体根目录中。通过图像路径对该图像的任何Web请求绕过django并由Apache直接提供服务。
如何确保只有有权请求图片的用户才能真正获得这些图片?
答案 0 :(得分:2)
为服务图像添加新视图并将图像存储在其他路径中,apache cant服务器新路径
现在在新视图中检查用户服务图像组 如果不是,您的用户发送403
@login_required
def serve_file(request, context):
if <check if they have access to the file>:
filename = "/var/www/myfile.xyz"
response = HttpResponse(mimetype='application/force-download')
response['Content-Disposition']='attachment;filename="%s"'%filename
response["X-Sendfile"] = filename
response['Content-length'] = os.stat("debug.py").st_size
return response
return <error state>