【发布时间】:2020-02-16 22:11:30
【问题描述】:
我正在构建一个应允许用户使用 Django 上传和下载文件的应用程序。
我有一个存储文件的模型:
class Document(models.Model):
description = models.CharField(max_length=255, blank=True)
unit_code = models.CharField(max_length=6)
input = models.FileField(upload_to='documents/')
uploaded_at = models.DateTimeField(auto_now_add=True)
@property
def filename(self):
return os.path.basename(self.document.name)
我可以从我的视图中显示我的模板中上传的文件列表:
def Document(request, str):
documents = Document.objects.filter(unit_code=str)
并在模板中使用here所见的url下载文件的锚标记:
{% for document in documents %}
{{document.filename}}<a href="{{document.input.url}}"> Download Document </a>
{% endfor %}
现在我收到一个错误找不到页面(网址指向127.0.0.1:8000/media/documents/file_name
是否可以通过 Django 开发服务器使用锚标签下载文件?
【问题讨论】:
标签: django django-templates django-views