0

我的一个 Django 模型中有一个 ImageField。这些图像中的每一个都有一个可以访问它们的用户(或一组用户);没有其他用户应该能够看到它们。

ImageField 将图像文件存储在媒体根目录中。通过图像路径对该图像的任何 Web 请求)绕过 django 并直接由 Apache 提供服务。

如何确保只有被授权请求图像的用户才能真正获得它们?

4

1 回答 1

2

为服务图像添加新视图并将图像存储在其他路径中,apache 无法服务器新路径

现在在新视图中检查用户服务图像组,如果不是您的用户发送 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>
于 2012-08-15T03:04:59.423 回答