如何在 Django Admin 的 FileField 中显示附加文件的下载链接?

发布于 2024-12-14 13:46:05 字数 307 浏览 2 评论 0原文

我的 django 模型中有 FileField:

file = models.FileField(upload_to=FOLDER_FILES_PATH)

在用于更改此模型的 Django 管理部分中,我有此文件的完整路径(默认情况下):

Currently: /home/skyfox/Projects/fast_on_line/order_processor/orders_files/mydog2_2.jpg 

如何为我的管理面板用户显示下载此文件的链接?

I have FileField in my django model:

file = models.FileField(upload_to=FOLDER_FILES_PATH)

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

箹锭⒈辈孓 2024-12-21 13:46:05

例如,如果您有一个模型“案例”,您可以向您的类添加一个方法,该方法“创建”指向上传文件的链接:

from django.utils.html import format_html

class Case(models.Model)
    ...
    file = models.FileField(upload_to=FOLDER_FILES_PATH)
    ...

    def file_link(self):
        if self.file:
            return format_html("<a href='%s'>download</a>" % (self.file.url,))
        else:
            return "No attachment"
    
    file_link.allow_tags = True

然后在您的 admin.py 中

list_display = [..., file_link, ...]

If you have a model "Case" for example, you could add a method to your class which "creates" the link to the uploaded file :

from django.utils.html import format_html

class Case(models.Model)
    ...
    file = models.FileField(upload_to=FOLDER_FILES_PATH)
    ...

    def file_link(self):
        if self.file:
            return format_html("<a href='%s'>download</a>" % (self.file.url,))
        else:
            return "No attachment"
    
    file_link.allow_tags = True

then, in your admin.py

list_display = [..., file_link, ...]
无畏 2024-12-21 13:46:05

你可以简单地通过更改 admin.py 来做到这一点,

from django.contrib import admin
from app.models import *

class AppAdmin(admin.ModelAdmin):
    list_display = ('author','title','file_link')
    def file_link(self, obj):
        if obj.file:
            return "<a href='%s' download>Download</a>" % (obj.file.url,)
        else:
            return "No attachment"
    file_link.allow_tags = True
    file_link.short_description = 'File Download'

admin.site.register(AppModel , AppAdmin)

you can simply do this by changing admin.py,

from django.contrib import admin
from app.models import *

class AppAdmin(admin.ModelAdmin):
    list_display = ('author','title','file_link')
    def file_link(self, obj):
        if obj.file:
            return "<a href='%s' download>Download</a>" % (obj.file.url,)
        else:
            return "No attachment"
    file_link.allow_tags = True
    file_link.short_description = 'File Download'

admin.site.register(AppModel , AppAdmin)
南…巷孤猫 2024-12-21 13:46:05

注意:此解决方案仅适合开发使用。

另一个解决方案是简单地将以下内容添加到 urls.py 文件中:

from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
    ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这将允许管理 /change/ 页面上提供的默认链接提供文件下载服务。

文档 在这里

有关如何在生产中提供这些文件的信息,请参阅文档此处。

NOTE: This solution is only appropriate for development use.

Another solution is to simply add the following to your urls.py file:

from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
    ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

This will allow the default link provided on the admin /change/ page to serve the file for download.

Docs here.

For information on how to serve these files in production see docs here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文