从html代码调用python函数

时间:2014-11-18 18:11:50

标签: javascript python ajax google-app-engine flask

我现在正在使用flask框架开发谷歌应用程序引擎我已经创建了一个具有不同控件的模板我不需要从我的html中调用python中的函数,其中我通过ajax脚本抓取按钮触发器html代码为按照

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

我的python代码是

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 =&#39; 0.0.0.0&#39;,port = 4567)#we使用run()函数用我们的应用程序运行本地服务器

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

if __name__ == '__main__':
main()

但是python函数没有返回TARIQ_ID的更新值,也没有做出任何回应请你的帮助

2 个答案:

答案 0 :(得分:1)

我不知道Flask,所以这只是一个猜测,但你需要改变这个:

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

到此:

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

答案 1 :(得分:0)

$ .ajax()调用中的Javascript函数insert_fun()需要回调函数。

https://api.jquery.com/jQuery.ajax/中的示例在$ .ajax()调用后显示'.done(some_callback_function)'。