gpt4 book ai didi

javascript - 从html代码调用python函数

转载 作者:行者123 更新时间:2023-12-03 11:23:50 25 4
gpt4 key购买 nike

我现在正在使用 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)))

定义主函数(): #app.debug = True #app.run(host='0.0.0.0', port=4567) #我们使用 run() 函数通过我们的应用程序运行本地服务器

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

if __name__ == '__main__':
main()

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

最佳答案

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

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

对此:

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

关于javascript - 从html代码调用python函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27001063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com