Django download file not working

后端 未结 2 873
臣服心动
臣服心动 2020-12-17 05:32

I\'m trying to make a script for downloading the uploaded files, on the user\'s machine. The problem is that the download simply doesn\'t work (it either downloads me an emp

2条回答
  •  隐瞒了意图╮
    2020-12-17 06:21

    Unless you are letting the user download a dynamically generated file, I don't see why you need to do all that.

    You can just let this view redirect to the appropriate path, and the respective headers are set by the server serving the static files; typically apache or nginx

    I'd do your this view as follows:

    from django.conf import settings
    
    def download_course(request,id):
        course = get_object_or_404(Course,id=id)
        filename = course.course
        return redirect('%s/%s'%(settings.MEDIA_URL,filename))
    

    Enjoy :)

提交回复
热议问题