如何使用django制作私人下载区? [英] How to make a private download area with django?

查看:116
本文介绍了如何使用django制作私人下载区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在由django提供支持的网站上实施私人下载区域。用户必须使用适当的权限登录才能获取一些静态文件。



您将建议您撰写此功能。任何提示或技巧?



提前感谢



更新:
也许是因为我的英文或者我对这个架构的了解不足(这就是为什么我问),但是我的问题是:如何确保静态文件(由普通webserver提供而不需要django)访问由django身份验证控制。
我会更仔细地阅读django文档,但我不记得这个问题的开箱即用的解决方案。



Update2:My主机提供商只允许FastCgi。

解决方案

所以,搜索我发现这个讨论话题。



有三件事情你可能会感兴趣。 >

首先有一个mod_python 方法

然后有mod_wsgi 方法



这两个都不是很好。



更好的是X-Sendfile头,不完全标准,但至少在apache和lighttpd中起作用。



这里,我们有以下内容。

  @ login_required 
def serve_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>

这几乎完全是你想要的。只需确保您随时可以使用X-Sendfile支持。


I would like to implement a private download area on a website powered by django. The user would have to be logged in with the appropriate rights in order to be able to get some static files.

What would you recommend for writing this feature. Any tips or tricks?

Thanks in advance

Update: Maybe because of my bad english or my lack of knowledge about this architecture (that's why I am asking) but my question is: how to make sure that static files (served by the regular webserver without any need of django) access is controlled by the django authentication. I will read the django docs more carefully but i don't remember of an out-of-the-box solution for that problem.

Update2: My host provider only allows FastCgi.

解决方案

So, searching I found this discussion thread.

There were three things said you might be interested in.

First there is the mod_python method
Then there is the mod_wsgi method

Both of which don't seem all that great.

Better is the X-Sendfile header which isn't fully standard, but works at least within apache, and lighttpd.

kibbitzing from here, we have the following.

@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>

and that should be almost exactly what you want. Just make sure you turn on X-Sendfile support in whatever you happen to be using.

这篇关于如何使用django制作私人下载区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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