【问题标题】:Flask webapp generating permanent report as linkFlask webapp生成永久报告作为链接
【发布时间】:2023-04-05 05:14:01
【问题描述】:

好的,我找不到任何符合我的问题或用例的描述。

我开发了一个烧瓶网络应用程序,用户可以在第一个模板中输入一些表单数据来进行数据库查询。根据查询结果呈现带有输出的第二个模板,它将以可读的方式呈现结果。

我的问题是,我想提供一个文档/报告的链接,有人只需打开该链接即可查看相同的显示报告。

或者将生成的 html 保存为 pdf 在谷歌驱动器上。

有什么建议可以解决这个问题吗?

当您看到示例代码时,我想打开 out.html,因为它是稍后使用特定用户输入呈现的。

我没有找到任何方法如何生成该链接,如果可能的话?


from flask import Flask
app = Flask(__name__)

@app.route('/start')
def start():
   return render_template("start.html")

@app.route('/out')
def out():
   input = request.form
   return render_template("out.html", input=input["Test"])

Start.html

  <!doctype html>
  <html>
  <body>
  <h1>Start</h1>

  <form action = "/out" method = "POST">
          <p>Test<input type = "text" name = "Test" required/></p>
          <p><input type = "submit" value = "SUBMIT" /></p>
      </form>
  </body>
  </html>

  '''

  '''out.html

  <!doctype html>
  <html>
  <body>
  <h1>Out</h1>

  <p>{{input}}</p>

  </body>
  </html>

【问题讨论】:

  • 你可以使用表单方法作为 GET 并在重定向后发送该链接
  • 我想我明白了,你的意思,但我不知道该怎么做。我正在考虑使用查询字符串来编写网站,但显然我可以在烧瓶中使用我的构建来完成。
  • 为什么您的表单操作是/checkspots 而不是/out
  • 我粗心的错误。感谢您指出。我已经更正了。

标签: python flask web-applications report permalinks


【解决方案1】:
from flask import Flask, request

app = Flask(__name__)

@app.route("/")
def index():
    return """<form action="/result" method="GET">
          <input type="text" name="test"><input type="submit"></form>"""

@app.route("/result")
def result():
    return request.args.get("test")
if __name__ == "__main__":
    app.run()

您可以从地址栏中复制链接,提供相同的结果。

编辑:

我无法理解您的问题。 所以我猜你的{{input}} 没有得到价值。 如果是这样,

您需要在后端执行此操作:

......
input = request.args.get("test")
return render_template("out.html", input=input)
......

编辑 2

或者您可以使用 sql 数据库为每个新输入值存储具有 16 位唯一 ID 的输入值。

....
@app.route("/<unique>")
def result(unique):
    #retrieve the input values from the database using the id
    # and render the template 

【讨论】:

  • 但是我失去了所有的用户输入。我不会看到表单“test”的输入。我需要做另一个输入。
  • 确实是使用requeest.args 从查询字符串计算输入的问题。做了一些转储错字,所以它一开始没有用。感谢您的回答。帮助我走上正轨。
猜你喜欢
  • 1970-01-01
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
相关资源
最近更新 更多