JavaScript django template cannot call javascript

I have the following django template and I have defined a button in it so that it calls the javascript function which I added to the page like below. I have the below issues:

1) If I bring the script tags to the header section nothings shows up.

2) If press the button I get the undefined error message. (Uncaught ReferenceError: postData).

What am I mssing here?

Thank you very much, Mike

newproduct.html

{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>





<title>New Product</title>
</head>  
<body>
<p>Hellow</p>
<a target='_blank' href="/">Back</a>

    <h2>Please enter product details:</h2>
    <button onclick="postData();">Send Post</button>


<form action="http://127.0.0.1:8000/newproduct/" method="post">
    <p>{{ message }}</p>
        {% csrf_token %}
        {{ form.as_p }}

    <input type="submit"/>
</form>


<script src="bower_components/jquery/src/jquery.js" type="text/javascript"/>
<script src="scripts/main.js" />

</body>
</html>

main.js

function postData ()
{
    $.post('/newproduct', {name:'New Product JS', price:555, });
};
Answer:1

You've hard-coded the reference to postData in your HTML, but you've used it before you've defined it, since the JS isn't loaded until the end of the page.

You could fix this by moving the script to the top of the HTML, but it is better not to use JS directly in HTML like this. Rather, your script itself should use the jQuery dom-ready functionality to bind itself to the button event.

html:

<h2>Please enter product details:</h2>
<button id="my_button">Send Post</button>

main.js

$(function() {
  $('#my_button').click(function() {
    $.post('/newproduct', {name:'New Product JS', price:555, });
  });
});
Answer:2

I am trying to make a table using jquery-datatables with the header shown below: <table id="tableau" class="display" width="100%" align="center"> <thead> <tr> <th ...

I am trying to make a table using jquery-datatables with the header shown below: <table id="tableau" class="display" width="100%" align="center"> <thead> <tr> <th ...

  1. issue when using

The explanation is a bit long so please bear with me. I am building a Facebook messenger bot which uses my sails.js/node.js server in the backend and a MongoDB database. In my sails app, I have ...

The explanation is a bit long so please bear with me. I am building a Facebook messenger bot which uses my sails.js/node.js server in the backend and a MongoDB database. In my sails app, I have ...

Currently, the string is Package A (123 queries) Package B (212 queries) Is it possible to use jquery to split it into new line like Package A (123 queries) Package B (212 queries) I ...

Currently, the string is Package A (123 queries) Package B (212 queries) Is it possible to use jquery to split it into new line like Package A (123 queries) Package B (212 queries) I ...

  1. jquery split string into array
  2. jquery split string by space
  3. jquery split string into array by comma
  4. jquery split string by pipe
  5. jquery split string by slash
  6. jquery split string by new line
  7. jquery split string into two variables
  8. jquery split string to array foreach
  9. jquery split string after character
  10. jquery split string by multiple characters
  11. jquery split string to integer array
  12. jquery split string by backslash
  13. jquery split string after number of characters
  14. jquery split string by dash
  15. jquery split string by length
  16. jquery split string by word
  17. jquery split string get last element
  18. jquery split string by string
  19. jquery split string by hyphen
  20. jquery split string by dot

I am unable to update the geodata array inside places.success function. console.log prints JSON object inside function, however outside function it's undefined. I want to access geodata array ...

I am unable to update the geodata array inside places.success function. console.log prints JSON object inside function, however outside function it's undefined. I want to access geodata array ...

  1. data saving service