Having Django serve downloadable files

后端 未结 15 1799
野的像风
野的像风 2020-11-22 05:46

I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded.

For instance, I\'d like the URL to be something

15条回答
  •  逝去的感伤
    2020-11-22 06:37

    Another project to have a look at: http://readthedocs.org/docs/django-private-files/en/latest/usage.html Looks promissing, haven't tested it myself yet tho.

    Basically the project abstracts the mod_xsendfile configuration and allows you to do things like:

    from django.db import models
    from django.contrib.auth.models import User
    from private_files import PrivateFileField
    
    def is_owner(request, instance):
        return (not request.user.is_anonymous()) and request.user.is_authenticated and
                       instance.owner.pk = request.user.pk
    
    class FileSubmission(models.Model):
        description = models.CharField("description", max_length = 200)
            owner = models.ForeignKey(User)
        uploaded_file = PrivateFileField("file", upload_to = 'uploads', condition = is_owner)
    

提交回复
热议问题