在Flask应用程序中将变量从后端传递到前端 [英] Passing a variable from the backend to the frontend in a Flask app

查看:183
本文介绍了在Flask应用程序中将变量从后端传递到前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我希望下面的代码将搜索从后端放到前端。

Basically I want the code below to put the "search" from the backend to the frontend.

我无法让我的烧瓶应用程序传递数据来自使用模板和简单的烧瓶结构后端到前端。

I am having trouble getting my flask app to pass data from the back end to the front end using templates and a simple flask structure.

我愿意接受有关更好方法的建议。此外@ Daniel Mesejo在我学习Flask和ajax / jquery方面也给了我很大的帮助。

I am open to suggestions for better ways. Also @ Daniel Mesejo has been a great help so far in my learning about Flask and ajax/jquery.

这里是我的app.py

here is my app.py

from scraper import scrape
from flask import Flask, render_template, jsonify, make_response, request
import json
app = Flask(__name__)

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

    entries = json.dumps(scrape("video games"))
    return render_template('index.html', entries= entries)

@app.route('/parse_data', methods=['GET', 'POST'])
def parse_data():
    if request.method == "POST":
        #data = request.form("blah")
        #print("blah")
        search = request.get_json()
        #new_search = json.dumps(scrape(data))
        return jsonify(search)
    return render_template('index.html')

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port=5000)

这是我的index.html

here is my index.html

<!DOCTYPE html>
<html>

<head>
    <title>Flask app</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

  </head>
<body>

  <div class="topnav">
    <a class="active" href="#home">Home</a>
    <a href="#about">About</a>
    <a href="#contact">Contact</a>
    <form class = "form" action="" method="POST">
      <input id ="textbox" name="textbox" type="text" placeholder="Search..">
      <button type="submit">submit</button>
    </form>
  </div>

  <p>you searched: {{search}} </p>

  <div id="div1">
  <p id="p1"></p>
  <p id="p2"></p>
  </div>

<script>

var value = $('.textbox').val();
//alert(value);
$("button").click(function (e) {
    e.preventDefault();
    var value = $("#textbox").val();
    alert(value);
    $.ajax({
      type: 'POST',
      url: "parse_data",
      data: JSON.stringify({"text" : value}),
      contentType: 'application/json; charset=utf-8',
      success: function(data){
        alert(JSON.stringify(data));
      }
    });
});


var jsonz = {{ entries|tojson }};



var s = JSON.parse(jsonz);



var i;
for (i = 0; i < s.length; i++) {
  var para = document.createElement("p");
  var node = document.createTextNode(s[i].product_name + "\n" + s[i].product_link);
  para.appendChild(node);

  var element = document.getElementById("div1");
  element.appendChild(para);
}




//document.getElementById("user").innerHTML =
//obj;
//"Name: " + obj.product_name + "<br>" +
//"Location: " + obj.product_link;
</script>



</body>
</html>


推荐答案

实现想要简单地改变你传递给的函数成功这样:

To achieve want that simply change the function you pass to success like this:

success: function (data) {
                $("#search-query").text("you search: " + data["text"]);
            }

并更改< p> 元素到< p id =search-query>你搜索了:< / p>

要了解有关Flask和Web开发的更多信息,我建议使用Flask Mega教程,这里

To learn more about Flask and web development in general I suggest the Flask Mega Tutorial, in here.

这篇关于在Flask应用程序中将变量从后端传递到前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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