0

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?

1
  • onclick="register()" Commented Sep 13, 2019 at 16:49

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.