Python Django-模板标签;加上;在javascript中

Python Django-模板标签;加上;在javascript中,python,django,Python,Django,如何在外部JavaScript文件中使用djangowith模板标记? 我试着这样使用它: <script> //some code // {% with user|get_song_rating:opts.id as rated %} {% if rated %} $off.unbind('click').unbind('mousemove').unbind('mouseenter').unbind('mouseleav

如何在外部JavaScript文件中使用django
with
模板标记? 我试着这样使用它:

<script>
    //some code
    //
    {% with user|get_song_rating:opts.id as rated %}
        {% if rated %}
           $off.unbind('click').unbind('mousemove').unbind('mouseenter').unbind('mouseleave');
           $off.css('cursor', 'default'); $on.css('cursor', 'default');
           $this.attr('title', 'Your rating: ' + rated.points.toFixed(1));
        {% endif %}
    {% endwith %}
</script>

//一些代码
//
{%与用户| get_song_rating:opts.id为rated%}
{%如果评级为%}
$off.unbind('click')。unbind('mousemove')。unbind('mouseenter')。unbind('mouseleave');
$off.css('cursor','default')$on.css('cursor','default');
$this.attr('title','Your rating:'+rated.points.toFixed(1));
{%endif%}
{%endwith%}

但它在控制台
未捕获的SyntaxError:uncontractedtoken with
中给出了错误。我认为问题是
,而
也是JavaScript的关键字,那么解决方案是什么呢?

如果您希望文件包含Django模板标记,那么它必须由Django模板引擎处理。创建一个新视图,为JavaScript源发送适当的
内容类型
标题,然后处理并输出包含JavaScript的模板。

不是这样,这是两个不同的层-Django在呈现html时不关心JavaScript。似乎您错误地使用了{%with%}。这就是我如何将
标记一起使用的方法。您看到语法中有任何问题吗?谢谢!它只是起作用了。您能告诉我如何将JavaScript变量
opts.id
传递到模板过滤器中吗?它现在引发异常
查找密钥选项失败
您不能。您需要使用AJAX将变量传递给视图,然后将结果作为JSON返回。