简体   繁体   English

Django - javascript中的模板标签“with”

[英]Django - template tag “with” in javascript

How can i use the django with template tag in external JavaScript file? 如何在外部JavaScript文件中使用with模板标记的django? I tried to use it like this: 我试着像这样使用它:

<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>

But it gives error in console Uncaught SyntaxError: Unexpected token with . 但它在控制台Uncaught SyntaxError: Unexpected token with出错Uncaught SyntaxError: Unexpected token with I think the issue is with is the keyword for JavaScript also, so what is the solution? 我认为这个问题是with为JavaScript中的关键字也,故该如何解决?

If you want your file to contain Django template tags then it must be processed by the Django template engine. 如果您希望文件包含Django模板标记,则必须由Django模板引擎处理。 Create a new view that sends the proper Content-type header for JavaScript source and then processes and outputs the template that contains JavaScript. 创建一个新视图,为JavaScript源发送正确的Content-type标头,然后处理并输出包含JavaScript的模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM