The question is partially related to this one here. In addition it should be noted that the notation used below is no safe. For more details see this blog post.
I have a Django project that references multiple JS files. Initial the contents of those files were in the HTML but it slowly became too cluttered.
The problem is that inside these files I am referencing image files, which are used for creating button elements (in my case easyButton
instances from Leaflet).
I am looking for a way to somehow fix these references. Note that I have called collectstatic
and my HTML is using {% load static %}
to load the static content. My JS files are also defined as Django template language variables themselves.
I am thinking of perhaps at least loading all the references to the static files inside the HTML and then using those inside my JS files.
Here is an example code:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/application/html; charset=iso-8859-1">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="#" />
<title>Test</title>
</head>
<body>
...
</body>
</html>
<script>
const TEST = {{ test }}; // The 'test' variable unwraps as "/home/user/.../img_default.png"
</script>
<!-- Try to use TEST inside setup.js -->
<script src="{% static 'setup.js' %}></script>
<script src="{% static 'main.js' %}></script>
where setup.js
consists of function declarations that are called inside main.js
, e.g.:
function add_misc_buttons() {
var btn_create_log = L.easyButton("<img src='" + TEST + "' style='width: 32px; height: 32px;'/>", function (btn, map) {
console.log("TODO Send debug information to the developer");
window.location.href = "mailto:admin@example.com?body=THIS%20IS%20A%20TEST";
});
...
}
with TEST
being passed from the HTML file and populated by Django (a typical value would be something like images/some_image.png
).
Needless to say this doesn't work. My main.js
(where all the functions are called) doesn't really detect the existence of TEST
. I even added a console
log message to display the value but nothing shows. Is there some way to do this? I am no JS and Django expert by far. I know that variables in general are visible between tags (if declared in a correct order). But it appears this does not apply when using a mix.