【问题标题】:How to add a value from html file with jinja2 into python array如何将带有jinja2的html文件中的值添加到python数组中
【发布时间】:2021-03-11 18:54:59
【问题描述】:

我有一个数组,里面有一些用户名。

users = ["foo", "bar"]

我正在制作一个烧瓶应用程序。我想到达那个数组,把一个值放到那个数组中。

{{ users.append(value) }}

但为此我需要在我的 html 文件中获取输入元素的值。

我该怎么做?

【问题讨论】:

  • 任何基本的烧瓶教程都将涵盖从表单中获取数据。精简版。在您的视图函数中 form = FormModel() 然后输入fromhtml = form.field1.data

标签: python html jinja2


【解决方案1】:

您可以使用 POST 从 HTML 文件中正常访问该值,然后手动将其附加到数组中:

<html>
   <body>
      <form action = "http://localhost:5000/" method = "post">
         <p>Enter Username:</p>
         <p><input type = "text" name = "nm" /></p>
         <p><input type = "submit" value = "Enter" /></p>
      </form>
   </body>
</html>

然后是你的python代码:

@app.route('/', methods = ['POST', 'GET'])
def append_to_array():
    users = ["foo", "bar"]
    if request.method == 'POST':
        user = request.form['nm'] # Search through the response for the value of 'nm'
        users.append(user)

【讨论】:

  • 我有一个错误提示'请求未定义\
  • 如果我是正确的,请求是烧瓶中的一个模块。所以如果你还没有导入它,你应该这样做。
  • 来自烧瓶导入请求
猜你喜欢
  • 2020-06-01
  • 2021-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-12
  • 2013-09-20
  • 2017-02-11
相关资源
最近更新 更多