从html代码调用python函数 [英] call python function from html code

查看:89
本文介绍了从html代码调用python函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用Flask框架开发Google App引擎,我已经创建了具有不同控件的模板,我不需要从html调用python中的python函数,而我可以通过ajax脚本来捕获按钮触发,如下所示的html代码

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>

我的python代码是

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 =真#app.run(host ='0.0.0.0',port = 4567)#我们使用run()函数在我们的应用程序中运行本地服务器

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

但是python函数没有返回TARIQ_ID的更新值,也没有任何响应,请您帮忙

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

推荐答案

我不了解Flask,所以这只是一个猜测,但是您需要更改吗:

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

对此:

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

这篇关于从html代码调用python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆