在外部JavaScript中使用django模板标签 [英] using django template tags inside external JavaScript

查看:90
本文介绍了在外部JavaScript中使用django模板标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的html页面中添加了一个js文件(例如:application.js),但是我无法在该js文件中使用django模板标签,是否有任何方法直接在外部js文件中使用django模板标签?

解决方案

只有您将像模板一样解析JS。



如果您需要JS中的某些变量(如 STATIC_URL ),可以使用

 < script> window.staticUrl ={{STATIC_URL}}< / script> 

 < html data-static-url ={{STATIC_URL}}> 

在您的基本模板中


i have included a js file(eg:application.js) inside my html page but i couldn't able to use django template tag inside that js file ,is there any way to use django template tag directly inside external js file?

解决方案

Only if you will parse JS like a template.

If you need some variable inside JS (like STATIC_URL), it is ok to use

<script>window.staticUrl = "{{ STATIC_URL }}"</script>

or

<html data-static-url="{{ STATIC_URL }}">

in your base template

这篇关于在外部JavaScript中使用django模板标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆