Django【admin】添加自定义功能-点击复制功能

具体效果

image

具体实现

admin.py

from django.utils.html import format_html

@admin.register(models.UsvLicense)
class UsvDataAdmin(admin.ModelAdmin):
    def copy_one(self, obj):

        return format_html(f"""
                            <input type="text" id="{obj.id}" value="{obj.uuid}" style="position: absolute; top: -10000px">
                            <a href="#" onclick="document.getElementById('{obj.id}').select(); document.execCommand('copy'); alert('已复制')">复制授权码</a>
                            """
                         )

    copy_one.type = 'success'
    copy_one.short_description = '操作'

    list_display = ["uuid", "user", "usv_class", "license_date", "copy_one"]
posted @ 2021-11-02 10:15  lisicn  阅读(371)  评论(0编辑  收藏  举报