【问题标题】:index.html not showing dataindex.html 不显示数据
【发布时间】:2022-01-27 05:59:39
【问题描述】:

这是一个简单的项目,但不知何故我有货。我正在尝试创建自己的 webhook 接收器服务器,当使用“/webhook”接收到 webhook 时,它会在 index.html 中的“/”处显示接收到的数据。从技术上讲,我只是希望烧瓶使用 '/' 处的 'index.html' 显示在 '/webhook' 收到的最后一个数据,但它不起作用。

这是我的烧瓶目录:

-server.py
-webhook.py
-/templates/index.html

这是我的 server.py 代码:

from flask import Flask, render_template, request, abort, json, redirect, url_for

app = Flask(__name__)

@app.route('/')
def hello():
    title = request.args.get('title', None)
    jsonfile = request.args.get('jsonfile', None)
    print("Recieved:", title, jsonfile, type(jsonfile)) 
    return render_template('index.html', title=title, jsonfile=jsonfile)

@app.route('/webhook', methods=['POST'])
def webhook():
    if request.method == 'POST':
        data = request.json       
        return redirect(url_for('hello', title='Result', jsonfile=data)), 'success', 200
    else:
        abort(400)

if __name__ == '__main__':
    app.run()

这是我的 webhook.py:

import requests,json

webhook_url = "http://localhost:5000/webhook"

data = {
    'fn_name': 'abc',
    'last_name': 'xyz'
}

r = requests.post(
    webhook_url, data=json.dumps(data),
    headers={
        'Content-Type': 'application/json'
        }
        )

这是我的 index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>{{ title }}</title>
  </head>
  <body>
    <div>
    {{ jsonfile }}
  </div>
  </body>
</html>

我在发送 webhook 时得到这个。

127.0.0.1 - - [26/Jan/2022 12:00:39] "POST /webhook HTTP/1.1" 500 -

TypeError: 'int' 对象不可迭代

让我知道你的想法。

【问题讨论】:

    标签: html flask jinja2


    【解决方案1】:

    您将这些放在函数之外,因此它试图返回所有三个并且不知道如何处理“200”:

    return redirect(url_for('hello', title='Result', jsonfile=data)), 'success', 200
    

    不确定“成功”和“200”是否是重定向函数的有效参数,但可以通过以下方式实现所需的结果:

    return redirect(url_for('hello', title='Result', jsonfile=data))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      相关资源
      最近更新 更多