0

我目前正在做一个可以存储文件的网络应用程序。我的班级大厅是这样的:

class Lobby(models.Model):

    (...)
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, help_text="Unique ID for this particular lobby across whole database")
    pdf = models.FileField(upload_to='./documents/',blank=True, null=True)

    (...)

    def file_link(self):
        if self.pdf:
            return "<a href='%s' download>Download</a>" % (self.pdf.url)
        else:
            return "No attachment"

    file_link.allow_tags = True
    file_link.short_description = 'file_link'
    (...)

当我实例化一个大厅时,我可以轻松上传我的 pdf 文件并将其放到正确的位置(在我的应用程序的根目录中名为“documents”的文件中)。

但问题是,当我尝试通过调用链接下载文件时:

  <p><strong>File:</strong> {{ lobby.file_link|safe}}</p> 

我没有收到文件,但找不到文件错误

Firefox doesn't find the file  http://127.0.0.1:8000/catalog/lobby/73d5aede-96af-445e-bec9-8c4522d37ce7/documents/the document.pdf

这就像 file_link 不发送文件存储的位置,而是发送到特定大厅页面的 URL 名称,然后是 URL。

我做错了什么??

谢谢。

编辑:通过在 HTML 中添加表单的 enctype='multipart/form-data' 属性来工作。

4

0 回答 0