[英]JavaScript snippet not working in Django template
It's been a while since I did Django and I found an example of calling a view from a button that I needed: 自从我执行Django以来已经有一段时间了,我找到了一个从所需按钮调用视图的示例:
How do I call a Django function on button click? 如何在按钮单击时调用Django函数?
I built my template to match the example given: 我构建了模板以匹配给定的示例:
<html>
<head>
<title>Tournament Registration</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function register() {
alert("Hi")
}
</script>
</head>
<body>
<p>{{ tournament.name }}</p>
<p>Start time: {{ tournament.start_time }}</p>
<button onclick="register">Register</button>
</body>
</html>
At first I just wanted it to pop an alert box so I know it's working at all. 起初,我只是想让它弹出一个警告框,所以我知道它完全可以正常工作。 When I click the button however, nothing happens, and I get no error message in console.
但是,当我单击该按钮时,什么也没有发生,并且在控制台中没有收到任何错误消息。 Am I able to call script tags in a Django template, or am I doing something else wrong here?
我可以在Django模板中调用脚本标签,还是在这里做其他错误?
You have been programming with Django for so long that you're forgetting that Javascript still requires the pesky brackets in templates. 您使用Django进行编程已有很长时间了,以至于忘记了Javascript仍然需要模板中的讨厌括号。 :) No worries, as I do it too.
:)不用担心,我也这样做。
<button onclick="register">Register</button>
needs to be <button onclick="register">Register</button>
必须为
<button onclick="register()">Register</button>
and your code will work fine. <button onclick="register()">Register</button>
,您的代码即可正常工作。 ` `
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.