It's been a while since I did Django and I found an example of calling a view from a button that I needed:
How do I call a Django function on button click?
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?
You have been programming with Django for so long that you're forgetting that Javascript still requires the pesky brackets in templates. :) No worries, as I do it too.
<button onclick="register">Register</button>
needs to be
<button onclick="register()">Register</button>
and your code will work fine. `
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.