在javascript中使用Django模板变量

时间:2022-11-13 01:22:31

I'm writing an app that needs dynamic javascript. I didn't know how to use Django template variable in javascript. when I searched I found some answeres like Django Template Variables and Javascript but I still have the problem. when I write this piece of code in my html :

我正在编写一个需要动态JavaScript的应用程序。我不知道如何在javascript中使用Django模板变量。当我搜索时,我发现了一些像Django模板变量和Javascript的回答,但我仍然有问题。当我在我的html中编写这段代码时:

        <script>jQuery(document).ready(function ($) {
           $(".nav").css({"opacity": "0.5"});
           $("#description").animate({opacity: '+=0.5'}, 10000);
           });
        </script>

every thing is fine. the navbar is transparent and <p id="description">{{description}}</p>is shown by jquery animate function. but when i change it to this:

一切安好。导航栏是透明的,

{{description}} 由jquery animate函数显示。但是当我改变它时:

      <script>jQuery(document).ready(function ($) {
           $(".nav").css({"opacity": "0.5"});
           var a = "{{description}}";
           $("#description").animate({opacity: '+=0.5'}, 10000);
           });
      </script>

the navbar is no more transparent and the description is no more shown. what is the problem ?

导航栏不再透明,不再显示说明。问题是什么 ?

p.s : I changed a to a = "{{blah}}"; and there is no problem with it. the problem apears when It is a real template variable.

p.s:我把a改为a =“{{blah}}”;并没有问题。当它是一个真正的模板变量时,问题出现了。

1 个解决方案

#1


1  

Use verbatim:

使用逐字:

Stops the template engine from rendering the contents of this block tag.

停止模板引擎呈现此块标记的内容。

A common use is to allow a JavaScript template layer that collides with Django’s syntax. For example:

常见的用途是允许JavaScript模板层与Django的语法冲突。例如:

{% verbatim %}
     {{if dying}}Still alive.{{/if}} 
{% endverbatim %}

#1


1  

Use verbatim:

使用逐字:

Stops the template engine from rendering the contents of this block tag.

停止模板引擎呈现此块标记的内容。

A common use is to allow a JavaScript template layer that collides with Django’s syntax. For example:

常见的用途是允许JavaScript模板层与Django的语法冲突。例如:

{% verbatim %}
     {{if dying}}Still alive.{{/if}} 
{% endverbatim %}