问题
I'm struggling with the following problem. I have a database model with FileField()
.
models.py
class InputSignal(models.Model):
input_file = models.FileField(upload_to='signals/', null=False, )
A view that displays records from this table. It also supports deleting specific rows.
views.py
def storage_list(request):
signals = InputSignal.objects.filter(author=request.user)
if request.method == 'DELETE':
id = json.loads(request.body)['id']
signal = get_object_or_404(InputSignal, id=id)
signal.delete()
return HttpResponse('')
else:
return render(request, 'storage_list.html', {'signals': signals})
Template for this view looks like this:
template.html
{% for signal in signals %}
<li>
<h4>{{ signal.name }}</h4>
<p class="mb-0 pb-0">Date of addition:
<b>{{ signal.add_date_pretty }}</b>
</p>
<p class="mb-1">Date of the last analysis: {{ signal.last_edit_date_pretty }}</p>
<h6 class="mb-1">Adnotations:</h6>
<p>{{ signal.adnotations | safe | linebreaks | truncatewords:16 }}</p>
<button class="btn btn-outline-warning mr-3">Download</button>
<button data-id="{{ signal.id }}" onclick='delteSignal(this)' class="btn btn-outline-danger">Delete</button>
</li>
{% endfor %}
What I would like to do is to download the file in signal.input_file
via the browser to the disk after clicking the 'Download' button.
回答1:
<a href="{{ signal.input_file.url }}"><button class="btn btn-outline-warning mr-3">Download</button></a>
you can use this for the download.
来源:https://stackoverflow.com/questions/52467464/django-download-file-from-filefield