如何在 Django Admin 的 FileField 中显示附件的下载链接? [英] How to show download link for attached file in FileField in Django Admin?
本文介绍了如何在 Django Admin 的 FileField 中显示附件的下载链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 django 模型中有 FileField:
I have FileField in my django model:
file = models.FileField(upload_to=FOLDER_FILES_PATH)
在用于更改此模型的 Django 管理部分中,我有此文件的完整路径(默认情况下):
In Django admin section for changing this model I have full path to this file (by default):
Currently: /home/skyfox/Projects/fast_on_line/order_processor/orders_files/mydog2_2.jpg
如何为我的管理面板用户显示下载此文件的链接?
How can I show link for downloading this file for my admin panel users?
推荐答案
例如,如果你有一个模型案例",你可以在你的类中添加一个方法来创建"上传文件的链接:
If you have a model "Case" for example, you could add a method to your class which "creates" the link to the uploaded file :
class Case(models.Model)
...
file = models.FileField(upload_to=FOLDER_FILES_PATH)
...
def file_link(self):
if self.file:
return "<a href='%s'>download</a>" % (self.file.url,)
else:
return "No attachment"
file_link.allow_tags = True
然后,在你的 admin.py 中
then, in your admin.py
list_display = [..., file_link, ...]
这篇关于如何在 Django Admin 的 FileField 中显示附件的下载链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文