如何使Django的专用下载区? [英] How to make a private download area with django?

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

问题描述

我想实现搭载Django的网站上的私人下载专区。用户必须先登录,以便适当的权限能够得到一些静态文件。

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?

在此先感谢

更新:
也许是因为我的英语不好还是我缺乏对这种架构的知识(这就是为什么我问),但我的问题是:如何确保静态文件的访问(无需任何的Django的由正规Web服务器服务)是由控制Django的认证。
我会更仔细地阅读Django文档,但我不记得了这个问题一个彻头彻尾的即装即用的解决方案。

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:我的主机商只允许FastCGI的。

Update2: My host provider only allows FastCgi.

推荐答案

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

So, searching I found this discussion thread.

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

There were three things said you might be interested in.

首先是
方法/>
再有就是mod_wsgi的方法

这两者似乎不是所有的伟大。

Both of which don't seem all that great.

更好是X SENDFILE头这是不充分的标准,但至少在阿帕奇作品,并lighttpd的

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

从这里 kibbitzing,我们有以下几点

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>

这应该是几乎正是你想要的。只要确保你打开的X SENDFILE支持,无论你碰巧使用。

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天全站免登陆