javascript - django模板调用js对象
问题描述
如何在 django 中执行此操作。
<body onload="main_(1);"> </body>.
我用这个试过了。
{ load static }
<body>
{% if true %}
<script src="{% static 'main/js/main_index.js' %}" ></script>
<script>
main_(1);
</script>
{% endif %}
</body>
到目前为止没有运气。有没有更简单的方法。
解决方案
在您的if
模板标签中:它True
不是true
.
尝试:
{ load static }
<body>
{% if True %}
<script src="{% static 'main/js/main_index.js' %}" ></script>
<script>
main_(1);
</script>
{% endif %}
</body>
或者:
{ load static }
<body onload="main_(1);">
<script src="{% static 'main/js/main_index.js' %}" ></script>
</body>
确保文件main_index.js
已加载到浏览器 devtools 的网络选项卡中
我希望这将有所帮助。
推荐阅读
- c# - ASP.NET Core 将大型视频文件流式传输/加载到前面,而不会出现内存溢出或临时文件
- python - 使用另一个列表的值作为参考替换列表元素的值
- google-apps-script - try/catch 不适用于 google 脚本单元格验证错误
- javascript - Django Rest Framework - 获取相关的 FK 对象以在模板中使用;POST现在不起作用?
- python - 数据框 Python 的 Bigrams
- postgresql - 如何让函数在 Postgresql 中的 DO 语句中工作
- python - 通过flutter DIO包调用Post请求时抛出未处理的异常错误
- python - 如何将 PIL 图像从 python 发送到 Node.js?
- php - Mysql数据库插入“空白”数据而不是用户通过php连接输入的实际数据
- angular - 增加字数限制
在角度