Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将变量从Python Flask发送到HTML_Python_Html_Flask_Python Requests - Fatal编程技术网

将变量从Python Flask发送到HTML

将变量从Python Flask发送到HTML,python,html,flask,python-requests,Python,Html,Flask,Python Requests,我有以下问题: 我想用Html中的按钮点击从Python发送一些数据到Html,但是我点击了按钮,它根本不起作用。 这是我的密码: * python.py: 这是我的Html: jQuery示例 {get_Flash_messages()%中的消息为%s} {{message}} {%endfor%} 烧瓶中的jqueryajax。单击按钮执行功能 $(function(){$(“#mybutton”)。单击(函数(事件){$.getJSON('/SomeFunction',{},函数(数据)

我有以下问题: 我想用Html中的按钮点击从Python发送一些数据到Html,但是我点击了按钮,它根本不起作用。 这是我的密码: *

python.py:

这是我的Html:


jQuery示例
{get_Flash_messages()%中的消息为%s}
{{message}}
{%endfor%}
烧瓶中的jqueryajax。单击按钮执行功能
$(function(){$(“#mybutton”)。单击(函数(事件){$.getJSON('/SomeFunction',{},函数(数据){});返回false;});
文本:{{text}


所以我想要的是,当我按下输入按钮时,它将显示python中定义的名称。也许还有其他方法可以让它工作,但我需要的正是python。

好的,所以将变量从python flask传递到html很容易。 导入json

创建一个包含key的dictionary对象,并将变量分配给该key jobj={'name':name} 现在将此对象作为json转储,并作为参数传递 返回render_模板('htmlfile.html',res=json.dumps(jobj))

现在,您可以在htmlfile.html中访问JavaScript中的res json

Var res=JSON.parse('{res | safe}}'); console.log(res.name)

from flask import Flask, render_template, flash, Markup, request
app = Flask(__name__)


@app.route('/')
def index():
    return render_template('new.html')

@app.route('/SomeFunction', methods = ['POST', 'GET'])
def SomeFunction():
    if request.method == 'GET':
         text = 'Name'  
    print("result:")
    return render_template("new.html",text = text)

if __name__ == '__main__':
   app.run()
<!doctype html>
<html>

<head>
    <title>The jQuery Example</title>
<div class="flashes">
  {% for message in get_flashed_messages()%}
    {{ message }}
  {% endfor %}
</div>
    <h2>jQuery-AJAX in FLASK. Execute function on button click</h2>  
<script type="text/javascript" {{ url_for('static', filename='app.js')}}></script>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script>
    <script type=text/javascript> $(function() { $("#mybutton").click(function (event) { $.getJSON('/SomeFunction', { }, function(data) { }); return false; }); }); </script> 

</head>

<body>        
        <input type = "button" id = "mybutton" value = "Click Here" />
    <p>text: {{text}}</p>

</body>    

</html>