Can you use var in python with jquery

What I am trying to do is make it so that the server.py script ie the def conutme() to refresh the page count as the page refreshes. and raises the counter. I currently have the counter showing in the . I need to know if its possible to use the j query that I have there that currently adds 2 and resets to 1 edit the session counter on click in the html. So in the end the server updates the count as the page refreshes off the session count. and then I can edit the number with the j query. The task is as follows

Create a simple web application that holds a counter that increments every time the page is visited. Complete this using session. For ninjas: add a +2 button underneath the counter that increments the counter by 2 and reloads the page. For hackers: add a reset button that will reset the counter to 1

Html

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <meta charset="utf-8">
  <link rel="stylesheet" type="text/css" href="stylenew.css"></style>
  <script type="text/javascript" src='http://code.jquery.com/jquery-1.10.2.min.js'></script> 
  <script>
 $(document).ready(function() {
    $(".my_button").on("click", function(event){

      event.preventDefault();

      var $button = $(this);

      var oldValue = $('#value');
      var newVal;
      if ($button.find('.hide').text() == "+") {
           newVal = parseFloat(oldValue.val()) + 2;
        } else {
       // Don't allow decrementing below zero
        if (oldValue.val() < 0) {
          newVal = parseFloat(oldValue.val()) - 1;
        } else {
          newVal = 1;
        }
      }

      oldValue.val(newVal);

    });
});


  </script>

</head>
<body>
<h1>{{session['counter']}}</h1>
<div>
<label for="value">Counter</label>
<input type="text" class="cat_textbox" id="value" name="value" maxlength="4000" value="0" />
 <ul class="button-group button-click">
  <li><a href="#" class="small button secondary my_button"><i class="fa fa-plus"><span class="hide">+</span></i></a></li>
  <li><a href="#" class="small button secondary my_button"><i class="fa fa-minus"><span class="hide">-</span></i></a></li>
 </ul>
 </div>
</body>
</html>

Server.py

from flask import Flask, render_template, request, redirect, session
app = Flask(__name__)
app.secret_key = 'ThisIsSecret' # you need to set a secret key for security purposes
# routing rules and rest of server.py below
@app.route('/')
def index():


   session['counter'] = 1

   return render_template("index.html")


def countme():

   if session['counter'] in session:
     counter + 0
   else:
      session['counter'] = session['counter'] + 1


app.run(debug=True)

Answers 1

  • As you are using it in script block you can use var for jquery or javascript. you cant use it in Server.py


Related Articles