1

I'm now working to develop google app engine using flask framework i have create a template with different control i need no to call function in python from my html where i catch the button trigger by ajax script the html code as follow

 <html>

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" ></script> 

 <br>
 <form    action="" method="post" >
 <div id ="par_input" ><table> <tr> <td><textarea id ="equation" rows="2" cols="60"></textarea>        <input id ="insert" type="button" onclick="insert_fun()" value="Insert"  > <!-- -->

</form>


</div>


<script type="text/javascript">

function insert_fun(){
//window.alert("Tariq work !");
$.ajax({
    type: 'POST',
    url: window.location.href + '/insert'
    });
    document.getElementById("equation").value = "{{ TARIQ_ID }}";
}


</script>

</html>

and my python code is

from flask import Flask                     #import the Flask class.
from flask import make_response
from flask import render_template
from flask import request
from flask import session
from flask.kvsession import KVSessionExtension
from flask import abort
from flask import jsonify



from google.appengine.ext import webapp 
from google.appengine.ext.webapp import template 

APPLICATION_NAME='application test '

app = Flask(__name__)   

@app.route('/', methods=['GET'])
def index():



 """Initialize a session for the current user, and render index.html."""
 TARIQ_ID = 'inside index function'


 # serving it.
 response = make_response(
                render_template('tariq.html',
                  APPLICATION_NAME=APPLICATION_NAME,
                  TARIQ_ID=TARIQ_ID))
 response.headers['Content-Type'] = 'text/html'
 return response

@app.route('/', methods=['POST'])
def insert(self = webapp.RequestHandler):   

   TARIQ_ID = 'In Python Code Function__INSIDE RESPONSE___===='
   response = make_response(
                render_template('tariq.html',
                  APPLICATION_NAME=APPLICATION_NAME,
                  TARIQ_ID=TARIQ_ID))
   response.headers['Content-Type'] = 'text/html'
   return response



class MyHandler(webapp.RequestHandler): 

 def get(self): 
    TARIQ_ID = 'In Python Code Function__INSIDE get===='
    self.response.out.write(unicode(
        template.render('tariq.html',
                                TARIQ_ID)))

 def post(self):
    TARIQ_ID = 'In Python Code Function__INSIDE post===='
    self.response.out.write(unicode(
        template.render('tariq.html',
                                TARIQ_ID)))

def main(): #app.debug = True #app.run(host='0.0.0.0', port=4567) #we use the run() function to run the local server with our application

 app = webapp.WSGIApplication([
    (r'.*',MyHandler)], debug=True)
wsgiref.handlers.CGIHandler().run(app)

if __name__ == '__main__':
main()

but the python function dose not return the updated value of TARIQ_ID also it dose not make any response please your help

2 Answers 2

1

I don't know Flask so this is just a guess, but do you need to change this:

@app.route('/', methods=['POST'])
def insert(self = webapp.RequestHandler):

to this:

@app.route('/insert', methods=['POST'])
def insert(self = webapp.RequestHandler):
Sign up to request clarification or add additional context in comments.

Comments

0

A callback function is needed in the Javascript function insert_fun() in the $.ajax() call.

The examples in https://api.jquery.com/jQuery.ajax/ show '.done(some_callback_function)' after the $.ajax() call.

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.