【问题标题】:I need to be able to redownload a document that was submitted onto my django website我需要能够重新下载提交到我的 django 网站上的文档
【发布时间】:2015-10-11 11:20:18
【问题描述】:

我是 django 的初学者。我正忙于一个用户可以提交论文的网站。那么审稿人(其他用户)以及提交论文的用户应该能够下载提交的论文(用于编辑和评分)吗?我如何让这成为可能?上传文档后,它会为我提供一个链接,该链接是文件存储位置的文件路径。当我点击它时,它只显示一条 404 消息,因为显然我没有带有该文件路径的网页或 URL。我该如何解决这个问题?

更新

我看过django提供的path()函数,但是不知道在哪里用?

更新

好的,所以我已将views.py 文件编辑为如下所示

def returnDoc(request):
    # This function get the documents of all the users and the in the html displays only the logged in authors submitted papers.
    doc = Document.objects.all()
    user = RegUser.objects.all()
    status = user

    return render_to_response('user_profiles/profile.html', {'doc':doc, 'status':status}, context_instance=RequestContext(request))

def download_paper(request, paper_pk):
    # Document is the model with the FileField.
    paper = get_object_or_404(Document, pk=paper_pk)

    with paper.pdf_file_field.open("r") as fd:
        response = HttpResponse(fd.read(), content_type="application/pdf")
        response['Content-Disposition'] = 'attachment; filename="%s"' % paper.pdf_file_field.title
        return response

那么我的模板如下所示:

`

        <p class="well"> Here you should be able to see any information regarding your submitted papers, including any reviews that have been made on these papers
            and the status of your paper as "accepted" or "rejected". Please note that all decisions made are final. </p>           

    <div class="jumbotron">
        <h2> Paper Submissions </h2>

            <!-- Display an author's papers (if they have any -->
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th> Paper Title </th>
                        <th> Institution </th>
                        <th> Abstract </th>
                        <th> File </th>
                        <th> Reviews* </th>
                        <th> Avg. Score </th>
                        <th> </th>
                    </tr>
                </thead>

            <!-- Iterate through each element of the Document() model and output each element in a table cell-->
            {% for item in doc %}
                {% if request.user == item.author %}
                <tr>
                    <td> {{ item.title }}</td>
                    <td> {{ item.institution }} </td>
                    <td> {{ item.abstract }} </td>
                    <td> <a href="{{MEDIA_URL}}{{item.file}}" target="_blank"> {{item.title}}</a></td>
                    <td> N.A </td>
                    <td> */10 </td>
                    <td> <a href="#"> Edit Paper </a> 
                </tr>

                {% endif %}

            {% endfor %} 

`

我不确定"{{MEDIA_URL}}{{item.file}}" 是否应该在其中,因为它只是将我重定向到 URL 本身中的文件路径。我应该把它拿出来吗?

提前谢谢你

【问题讨论】:

    标签: python django file download


    【解决方案1】:

    上传文件的“安全”下载是通过视图完成的。 (另一种方法是让网络服务器为所有上传的文件提供一些前缀,就像你提供任何静态文件一样。这样做的缺点是你失去了对文件的所有访问控制。任何人都可以阅读它们。)

    您将文件作为响应发送给客户端,而不是呈现模板。 (假设您使用的是FileField)。示例:

    def download_paper(request, paper_pk):
         paper = get_object_or_404(Paper, pk=paper_pk) # get your model instance
         # also check permissions on paper, if necessary
         with paper.pdf_file_field.open("r") as fd:
             response = HttpResponse(fd.read(), content_type="application/pdf")
             response['Content-Disposition'] = 'attachment; filename="%s"' % paper.pdf_file_field.name
             return response
    

    【讨论】:

    • 我找到了解决方案。我的文件字段存储在数据类型文件中,因此不需要打开。我也不能使用with,因为它需要__exit____enter__ 方法。将with 替换为:fd = paper.file
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 2011-03-14
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 2019-09-18
    相关资源
    最近更新 更多