How to use Django variable in JavaScript file?
You need to print it before your script tag
<html>
<head>
<script>
var username = {{user.get_username}};
</script>
<script src="/static/youtube.js"></script>
...
Then you can use the username
variable inside your youtube.js
as it is declared as a javascript global variable.
How to pass Django variable into Javascript
Thanks to Daniel Roseman, I found the answer and I overcome to display PDF cover page for each document.
This is my HTML file containing JS script :
{% 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 %}
It takes just 3-4s to load all PDF (maybe there is another way to do that faster), but it works !
Related Topics
Chart.Js Showing Time (Hh:Mm:Ss - 24 Hour Clock) on Xaxis
Slick Slider - How to Keep the Active Pagination (Dot) Always Centered
Filter on Multiple Columns Using One Pipe Angular 2
Sequelize - How to Return Json Objects of the Database Results Only
Is There a Way That I Can Check If a Data Attribute Exists
React Setstate Not Updating State and Ui
Electron - How to Add External Files
Can You Combine Multiple Images into a Single One Using JavaScript
Set Focus to the Next Input Element
Why Does Prettier Not Format Code in VS Code
Trigger Click in Typescript - Property 'Click' Does Not Exist on Type 'Element'
How to Convert Image to Byte Array Using JavaScript Only to Store Image on SQL Server
React | How to Add Dynamic New Divs Like Rows
Jquery Remove Special Characters from String and More
Select Drop Down Text-Align Center in Safari
Map, and Removing Commas from an Array in JavaScript
Onchange Event Updates State With 1 Character Delay
How to Compare Two Objects and Get Key-Value Pairs of Their Differences