【问题标题】:How to Return MEDIA URL in Template如何在模板中返回 MEDIA URL
【发布时间】:2023-03-21 08:00:01
【问题描述】:

我创建此函数以返回 模板 中文件的 媒体 URL,但它以 HTML 格式显示,例如 http://example.com/history/('/media/exam/returns/files.png',)


    @property
    def ReturnAttachment(self):
        from ResultManagement.models import Result
        attachment = Result.objects.filter(examinee=self.examinee, exam=self.exam)
        attachment_url = attachment[0].attachment.url
        if self.hasReturnAttachment():
            #print("URL", attachment)
            return attachment_url,

我该如何解决? 提前致谢

【问题讨论】:

  • 去掉末尾的逗号,在return attachment_url中。

标签: django django-models django-views django-templates


【解决方案1】:

该对象由末尾的尾随逗号包裹在一个单例元组中。因此,您应该删除 return attachment_url 行末尾的逗号:

@property
def ReturnAttachment(self):
    if self.hasReturnAttachment():
        from ResultManagement.models import Result
        attachment = Result.objects.filter(examinee=self.examinee, exam=self.exam)
        attachment_url = attachment[0].attachment.url
        return attachment_url

【讨论】:

    猜你喜欢
    • 2015-07-12
    • 2017-07-01
    • 1970-01-01
    • 2021-09-11
    • 2022-09-29
    • 2019-05-27
    • 2013-11-24
    • 1970-01-01
    • 2014-11-27
    相关资源
    最近更新 更多