在template中我们可以定义javascript变数为djagno变数,如下:
<script> let yourhouse = {{ variable }} function ShowHouses(){ document.getElementById("house").textContent=youhouse; }</script>
但如果把上面的内容独立成.js档案,这时原本的{{ variable }}就会出现问题了,因为它属于django的语言,javascript无法解读,这时其中一个最简单的方法是:
<script> let yourhouses = {{ variable }}</script><script>src="{% static 'js/house.js' %}"</script>
你还是必须在提前在template定义你的javacript变数为{{ variable }},然后再加入javascript档案路径。