【发布时间】: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