后台有时会有需要复制一些内容,所以需要一键复制功能来方便复制.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| from django.contrib import admin from django.utils.html import format_html
from .models import Note
@admin.register(Note) class NoteAdmin(admin.ModelAdmin): list_display = [..., "one_click_copy" ]
@admin.display(description='操作') def one_click_copy(self, obj): return format_html(f""" <a id="{obj.id}" data-value="{obj.serial_no}" href="javascript:;" onclick=";let text=document.getElementById('{obj.id}').getAttribute('data-value'); navigator.clipboard.writeText(text);">一键复制</a> """)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @admin.register(Note) class NoteAdmin(admin.ModelAdmin): list_display = [..., "one_click_copy" ]
@admin.display(description='操作') def one_click_copy(self, obj): return format_html(f""" <a id="{obj.id}" data-value="{obj.serial_no}" href="javascript:;" onclick="; const oInput = document.createElement('textarea'); const text=window.location.protocol+'//'+window.location.host+'/core/note/detail/' +document.getElementById('{obj.id}').getAttribute('data-value'); oInput.value = text; oInput.readOnly = 'readOnly'; document.body.appendChild(oInput); oInput.select(); document.execCommand('copy'); oInput.blur(); document.body.removeChild(oInput);">一键复制</a> """)
|
参考链接