この件に関する詳細をあまり見つけることができなかったので、質問に答えようと思いました。残念ながら、ブラウザーが pyperclip をサポートしているようには見えないため、HTML + Javascript の回避策が必要です (pyperclip を意味します)。最初に、Django テンプレート変数をそこから HTML 属性として追加します。Javascript を使用してコピー機能を処理できます。以下はこれを行う方法の例です。スタックオーバーフローが例に奇妙なフォーマットを与えていたため、事前に申し訳ありません. また、email_list_clipboard の ID を持つ以下のフォームがあることも前提としています。これが、同様の問題に遭遇する可能性のある他の人に役立つことを願っています!
例:
<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>