找到你要的答案

Q:Django: How to keep JavaScript code separate in static folder when it uses a python variable

Q:Django:如何让JavaScript代码静态文件夹分开的时候它用Python变量

I have a bunch of canvases that needs to be rendered. I don't want put the JavaScript code in a python loop, because I feel it's bad practice

home.html

{% load staticfiles %} 
<head>
    <script type="text/javascript" src="{% static 
    'canvas/canvasrender.js'   %}"></script>        
</head>

{% for canvas in user_canvas_set %}

    <canvas  class = "canvas" id="{{canvas.id}}" width="400" height="200" 
    canvas_post= ""{{canvas.canvas_post}}"" >
    Your browser does not support the HTML5 canvas tag.</canvas>

    **<script>
    canvasrenderNP.canvaswrite("{{canvas.id}}","{{canvas.canvas_post}}")
    </script>** 

{% endfor %}

I made a custom function that returns an array of the users canvas ids, because I use the canvas id as the id for the canvas element.

home.html

<script>
        alert("{{user_canvas_ids}}")
</script>

I get the desired output:

[247, 248, 251, 252]

Now when I put this in a static file

canvasrender.js

alert("{{user_canvas_ids}}")

then load it into home.html

{% load staticfiles %} 
<head>
    <script type="text/javascript" src="{% static 
    'canvas/canvasrender.js'   %}"></script>        
</head>

output:

"{{user_canvas_ids}}"

I am confused to as what is going on. I thought that the script tag inserts the js file in between the "<script> </script>"

I know I can make a element like so and get the attribute, but I feel like that is not good practice, or is it fine?

<p id="canvas_ids" canvas_ids ="{{user_canvas_ids}}"> </p >

Is there anything else I can do so that I can avoid writing JavaScript code in the HTML file?

Why this is not a duplicate question. It does not have anything about why the linked js file in the html page can not keep reference to the python variable. But if the JavaScript is coded in the html page it can

I have a bunch of canvases that needs to be rendered. I don't want put the JavaScript code in a python loop, because I feel it's bad practice

home.html

{% load staticfiles %} 
<head>
    <script type="text/javascript" src="{% static 
    'canvas/canvasrender.js'   %}"></script>        
</head>

{% for canvas in user_canvas_set %}

    <canvas  class = "canvas" id="{{canvas.id}}" width="400" height="200" 
    canvas_post= ""{{canvas.canvas_post}}"" >
    Your browser does not support the HTML5 canvas tag.</canvas>

    **<script>
    canvasrenderNP.canvaswrite("{{canvas.id}}","{{canvas.canvas_post}}")
    </script>** 

{% endfor %}

我做了一个自定义函数,它返回用户画布ID的数组,因为我使用画布ID作为画布元素的id。

home.html

<script>
        alert("{{user_canvas_ids}}")
</script>

我得到想要的输出:

[247, 248, 251, 252]

现在当我把这个放在静态文件中

canvasrender.js

alert("{{user_canvas_ids}}")

then load it into home.html

{% load staticfiles %} 
<head>
    <script type="text/javascript" src="{% static 
    'canvas/canvasrender.js'   %}"></script>        
</head>

输出:

"{{user_canvas_ids}}"

我困惑于正在发生的一切。我认为脚本标签插入js文件在“& lt之间;script>;<;/脚本>;”

我知道我可以做出这样的元素,得到的属性,但我觉得这是不好的做法,还是罚款?

<p id="canvas_ids" canvas_ids ="{{user_canvas_ids}}"> </p >

Is there anything else I can do so that I can avoid writing JavaScript code in the HTML file?

为什么这不是一个重复的问题。它没有任何关于为什么链接的js文件的HTML页面可以不参考Python变量。但如果JavaScript是在HTML页面可以编码

answer1: 回答1:

As you mentioned in your own comment, you can save user_canvas_id in a js variable in home.html and access it in your js file. Something like this:

<head>
    <script>var user_canvas_id = "{{ user_canvas_id }}"</script>
    <script type="text/javascript" src="{% static 'canvas/canvasrender.js' %}"></script>
</head>

当你在你自己的评论所说,你可以保存在一个user_canvas_id home.html js变量和访问它在您的js文件。像这样的东西:

<head>
    <script>var user_canvas_id = "{{ user_canvas_id }}"</script>
    <script type="text/javascript" src="{% static 'canvas/canvasrender.js' %}"></script>
</head>
answer2: 回答2:

I am confused to as what is going on. I thought that the script tag inserts the js file in between the "<script> </script>"

Kinda, but this is done client-side, by the browser who has no idea about the special meaning of curly braces in Django templates.

And static in the context of Django means just that: That Django serves that file as-is, without running it through the template engine. (Which wouldn't help here anyway, as for the JavaScript file in isolation, the value of user_canvas_ids would be unknown, so the template engine couldn't substitute anything useful for it.)

我困惑于正在发生的一切。我认为脚本标签插入js文件在“& lt之间;script>;<;/脚本>;”

有点,但这是做客户端,通过浏览器谁不知道花括号在Django的模板的特殊意义。

在Django中静态的意思是:django服务文件,无需运行它通过模板引擎。(这不会帮助这里,作为隔离,JavaScript文件user_canvas_ids的价值将是未知的,所以模板引擎不能替代任何有用的东西了。)

javascript  python  html  django