【问题标题】:Update/filter table content with checkboxes using Flask使用 Flask 使用复选框更新/过滤表格内容
【发布时间】:2021-05-23 07:17:25
【问题描述】:

我在 pythonanywhere 上创建了一个网络应用程序,其中我使用烧瓶(jinja2 模板)显示了一个动态表。但是,我无法通过单击复选框来更新表格。

the table

如果我想过滤这个表(假设我只想显示萝卜和豌豆),什么也不会发生。如果我在本地主机上运行该程序,它可以完全正常工作并在选中框并发送表单后更新表格。我知道我必须更新我的 app.py 以某种方式重新路由页面,但我无法为这个特定的应用程序弄清楚。我使用请求和不同的路由尝试了很多事情,但到目前为止都没有成功。

我的 HTML 模板代码:

<!-- create checkboxes to let the users chose the crop type -->
<form action="" method="get">
    <input type="checkbox" name="field" value="PEAS">Peas</input>
    <input type="checkbox" name="field" value="OIL">Oil Seed Rape</input>
    <input type="checkbox" name="field" value="POTATO">Potatoes</input>
    <input type="checkbox" name="field" value="STRAW">Strawberries</input>
    <input type="checkbox" name="field" value="TURNIPS">Turnips</input>
  <input class="smallButton" type="submit" value="Go!">

</table>

还有我的 app.py:

app = Flask(__name__)

@app.route("/", methods=["GET", "POST"])
def print_html():
    inpFields = fieldsHtml()
    return render_template('fieldsTemp.html', fields=inpFields)
...
...
  # get the form entry from "fields.html" and assign the elements to a list, then to a string and add it to the SQL statement
    form = cgi.FieldStorage()
    field = form.getlist('field')
    input = "%' OR CROPS.NAME LIKE '%".join(field)

    conn = mysql.connector.connect(
        host= "xxxx.mysql.pythonanywhere-services.com", user="xxxx", password="xxxx", db="xxxx$fieldsFinds")
    c = conn.cursor()
    # c.execute("USE fieldsFinds")
    c.execute(
        f"SELECT * FROM FIELDS JOIN CROPS ON FIELDS.CROP = CROPS.CROP WHERE CROPS.NAME LIKE '%{input}%'")

请注意,代码的第二部分在 fieldsHtml() 函数中,我将其分配给 render_template。

非常感谢您的任何意见。

【问题讨论】:

    标签: python forms flask jinja2 pythonanywhere


    【解决方案1】:

    您在 print_html 中的第二行有一个 return 语句,因此之后不会执行任何操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      • 2015-05-13
      • 2012-06-12
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      相关资源
      最近更新 更多