如何将接收到的数组从函数传递到Flask中的另一个函数?

时间:2018-04-03 07:37:36

标签: html flask

我有一个烧瓶应用程序。在html文件(fixtures.html)中,我从名为fetchMatches()的函数中接收变量。 varibale的名称是realData。现在,我想将这个完整的变量传递给名为placePred

的文件

这是返回html模板的函数的代码:

@app.route("/fetchMatches")
def fetchMatches():
    if g.user:
        dateBuild = []
        wb = xlrd.open_workbook('betDeNoBanter.xlsx')
        today = datetime.date.today()
        for sheet in wb.sheets():
            dates = sheet.col_values(0, 1)
            i=1
            for date in dates:
                date = datetime.datetime(*xlrd.xldate_as_tuple(date, wb.datemode)).date()
                #if(str(date) != str(today)):
                if date-today <= datetime.timedelta(0):
                    i = i+1
                    continue
                if date-today == datetime.timedelta(1):
                    entry = {"dateId":i, "date":str(date)}
                    row = i
                    teamOne = str(sheet.cell_value(row, 1))
                    teamTwo = str(sheet.cell_value(row, 2))
                    gameWeek = str(sheet.cell_value(row, 3))
                    dateInsert = str(date)
                    info = {"row":row, "date": dateInsert, "teamOne": teamOne, "teamTwo": teamTwo, "gameWeek": gameWeek}
                    dateBuild.append(info)
                    i = i+1
                    continue
                if date-today > datetime.timedelta(1):
                    i = i+1
                    continue
            if len(dateBuild) == 0:
                return "no match next day"
            else:
                return render_template("fixtures.html", realData=dateBuild)
    else:
        return redirect(url_for("login"))

这是模板

<table>
{% for data in realData %}
    <tr>
        <td>{{ data["row"] }}</td>
        <td>{{ data["teamOne"] }}</td>
        <td>{{ data["teamTwo"] }}</td>
        <td>{{ data["gameWeek"] }}</td>
        <td>id:</td>
        <td>{{ data["row"] }}</td>
    </tr>
{% endfor %}
</table>
    <div>
        If you enter +(number), it means pred is for team one to win, -(number) is for team two to win.
        <form action="/placePred" method="post">
            <input type="number" name="pred">
            <input type="hidden" value={{ realData }} name="realData">
            <input type="submit">
        </form>
    </div>

这是我希望拥有变量(数组)的函数:

@app.route("/placePred", methods = ["POST"])
def placePreds():
    if g.user:
        email = g.user
        number = request.form["pred"]
        idList = []
        idsAllowed = request.form["realData"]
        for id in idsAllowed:
            idList.append(id)
        return jsonify({'devices': idList})
        if not(number in idListid):
            return "Invalid bet"
        cursor = conn.cursor()
        query = "select * from bets where email = '" + str(email) + "' and sheet_id = '" + str(abs(int(number))) + "'"
        cursor.execute(query)
        if cursor.rowcount != 0:
            return "You havev already placed a bet on this fixture, fuck off!"
        if int(number) > 0:
            teamWin = 1
        elif int(number) < 0:
            teamWin = 2
        else:
            return redirect(url_for("login"))

        query = "insert into bets values(default, '" + str(email) + "', '" + str(teamWin) + "', 20, " + str(abs(int(number))) + ")"
        cursor.execute(query)
        conn.commit()
        return redirect(url_for("fetchMatches"))
    else:
        return redirect(url_for("login"))

1 个答案:

答案 0 :(得分:0)

您需要定义route variable,以下是它的工作原理。我只展示了相关部分:

placePreds()必须接收数据,因此您需要定义一个变量:

@app.route("/placePred<data>", methods = ["POST"])
def placePreds(data):
    # do something with data here ...

在表单中传递realData,如下所示:

<form action="{{ url_for('placePreds', data=realData) }}" method="post">