0

I wanted to show my sound sensor readings from a django site (original code as posted in the link). Now as per the situation 2 of the accepted answer, I wanted to make a Javascript function which repeatedly calls the ajax_data function from views template.

But it seems that no repeated calls are being made. And no update in reading reflects either.

My django template till now:

<!doctype html>
<html>
  <head>
    <title>Noise measurement site</title>
    <script src="http://code.jquery.com/jquery-3.0.0.js"
     integrity="sha256-jrPLZ+8vDxt2FnE1zvZXCkCcebI/C8Dt5xyaQBjxQIo="
     crossorigin="anonymous"></script> 
    <script language="javascript" type="text/javascript">
        function updateValue() {
            $.ajax({
                    url:"D:/Python programs/django_projects/Noise_Measurement/noise_m/views.py/ajax_data/",
                    //success: updateValue(), for experimenting with a recursive call...
                });
        }
        $(document).ready(function(){
            var v = setInterval(updateValue,2000);
        });
    </script>
  </head>
    <body>
      <p>Hello there</p>
      <p>Present noise level : {{noise_level}} dB</p>
    </body>
</html>

(I have mentioned rest of the code in my previously asked question. I have read some of the answers on the platform but I'm not getting much results.)

Update

Sorry I made a mistake. I made slight changes in the code and posted output without that only. Now I have made the exact changes as in the previous part. But output is not sorted out yet. (Thanks to comment by CumminUp07)

enter image description here

Debanjan Dey
  • 115
  • 2
  • 15

1 Answers1

0

** Own answer on own question **

Oh sorry, actually it was a misunderstanding of the syntax from my side.

I was first supposed to create method in views.py, which will send the reading from the module taking it. Then for that method, I had to assign an url using path(), in a fashion like:

path('read', views.data_update1, name='readings'),

Then the ajax request was supposed to be made to read link:

  $.ajax({
    url: "read",
    dataType: "json",
    contentType: "application/json",
    success: function(r) { ... }
  });

Then this method is conveniently called using setInterval method.

But finally at the line, the {{ }} didn't help, so the div where the value was to be displayed was assigned an id, whose value was updated on each call of the method.

Debanjan Dey
  • 115
  • 2
  • 15