由于我找不到有关此主题的许多详细信息,因此我想我会回答我的问题。不幸的是,浏览器似乎不会支持 pyperclip,因此需要 HTML + Javascript 解决方法(即在 pyperclip 上)。首先,从那里添加您的 Django 模板变量作为 HTML 属性,您可以使用 Javascript 来处理复制功能。下面是如何执行此操作的示例,提前抱歉,因为 stackoverflow 为示例提供了一些奇怪的格式。它还假设您有一个下面的表单,其 id 为 email_list_clipboard。我希望这可以帮助其他可能遇到类似问题的人!
例子:
<html email-list="{{request.session.email_list}}">
<script>
$(document).ready(function () {
function copyTextToClipboard(text) {
var textArea = document.createElement("textarea");
// Place in top-left corner of screen regardless of scroll position.
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = '2em';
textArea.style.height = '2em';
// We don't need padding, reducing the size if it does flash render.
textArea.style.padding = 0;
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
textArea.style.background = 'transparent';
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
document.body.removeChild(textArea);
}
// set things up so my function will be called when field_three changes
$('#email_list_clipboard').click(function (click) {
event.preventDefault();
copyTextToClipboard(document.documentElement.getAttribute("email-list"));
});
</script>