我想获得一些帮助,将Django
模板中的HTML
变量传递到Javascript
变量中。
我相信这很容易,但是到目前为止,我还没有克服困难。特别是对于Django for loop.
我有一段这样的HTML代码:
{% for document in publication.documents.all %}
<table class="table table-condensed">
<tbody>
<tr>
<td class="col-md-1">
<canvas id="test{{ document.id }}"></canvas>
</td>
</tr>
</tbody>
</table>
{% endfor %}
然后,我想使用Javascript代码来显示每个文档的PDF。
所以,我在JS中有这样的东西:
<script src="https://mozilla.github.io/pdf.js/build/pdf.js"></script>
<script type="text/javascript">
var document_upload = "{{ document.upload }}"; //Django variable
pdfjsLib.getDocument('http://localhost:8000/media/'+document_upload).then(function (pdf) {
console.log("pdf loaded");
pdf.getPage(1).then(function (page) {
var scale = 0.30;
var viewport = page.getViewport(scale);
var canvases = document.getElementsByTagName('canvas');
Array.from(canvases).forEach((canvas) => {
var context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
var renderContext = {
canvasContext: context,
viewport: viewport
};
page.render(renderContext);
});
});
});
</script>
但是我的模板中没有任何内容。
如果我为此做console.log()
:
var document_upload = "{{ document.upload }}"; //returns blank
var document_upload = {{ document.upload }} //returns Undefined
也许有人可以帮助我?
我不知道,但我想我也需要根据此Django循环{% for document in publication.documents.all %}
在我的JS中添加一个循环?
答案 0 :(得分:0)
<script>
const document_upload = {{document | safe}}
console.log(document.upload)
</script>
检查是否有数据
如果有,它将打印数据,否则将为您提供一个空数组
答案 1 :(得分:0)
将django变量放在html内作为数据元素。然后运行循环以渲染pdf。您的变量仅在{% for %}
循环中可用,因此您可以将js放入循环中(不要这样做)或执行此操作。
我添加的脚本使用jQuery,所以请不要忘记添加它。
{% for document in publication.documents.all %}
<table class="table table-condensed">
<tbody>
<tr>
<td class="col-md-1">
<canvas class="my-canvas" data-upload="{{ document.upload }}" id="test{{ document.id }}"></canvas>
</td>
</tr>
</tbody>
</table>
脚本,最好在
之前
<script src="https://mozilla.github.io/pdf.js/build/pdf.js"></script>
// add jQuery
<script type="text/javascript">
$(document).ready(function(){
$('.my-canvas').each(function(){
var document_upload = $(this).data('upload'); // Django variable saved as data attribute
console.log(document_upload);
/* Do rest of your pdf.js stuff here */\
pdfjsLib.getDocument('http://localhost:8000/media/' + document_upload).then(function (pdf) {
console.log("pdf loaded");
pdf.getPage(1).then(function (page) {
var scale = 0.30;
var viewport = page.getViewport(scale);
var canvases = document.getElementsByTagName('canvas');
Array.from(canvases).forEach((canvas) => {
var context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
var renderContext = {
canvasContext: context,
viewport: viewport
};
page.render(renderContext);
});
});
});
});
});
</script>
答案 2 :(得分:0)
感谢Daniel Roseman,我找到了答案,并且克服了为每个文档显示PDF封面的问题。
这是我的包含JS脚本的HTML文件:
{% for document in publication.documents.all %}
<table class="table table-condensed">
<tbody>
<tr>
<td class="col-md-1">
{% if document.format == 'pdf' %}
<script src="https://mozilla.github.io/pdf.js/build/pdf.js"></script>
<script type="text/javascript">
var document_upload = "{{ document.upload }}";
pdfjsLib.getDocument('http://localhost:8000/media/'+document_upload).then(function (pdf) {
pdf.getPage(1).then(function (page) {
var scale = 0.30;
var viewport = page.getViewport(scale);
var canvas = document.getElementById('test{{ document.id }}');
var context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
var renderContext = {
canvasContext: context,
viewport: viewport
};
page.render(renderContext);
});
});
</script>
<canvas id="test{{ document.id }}" style="border:1px solid #000000;"></canvas>
{% else %}
<span class="glyphicon glyphicon-blackboard"></span>
{% endif %}
</td>
</tr>
</tbody>
</table>
{% endfor %}
加载所有PDF只需3-4秒(也许还有另一种方法可以更快地加载它),但是它可以正常工作!