【问题标题】:Python Flask Jinja SQL: No value being passed to templatePython Flask Jinja SQL:没有值被传递给模板
【发布时间】:2017-04-11 21:37:13
【问题描述】:

在我正在处理的 Flask 函数的 GET 部分中,我有一些用 Python 3 编写的非常简单的代码。我试图传入的数据永远不会显示在我的 HTML 渲染中。

@app.route("/sellselected", methods=["GET", "POST"])
@login_required
def sellselected(order_num):
    if request.method == "POST":
        #not done, just have a redirect to index
    else:
        stock_to_sell = db.execute("SELECT * FROM purchases WHERE order_num = :order_num", order_num=order_num)
        #stock_to_sell = ['fish']

    return render_template("sellselected.html", stock_to_sell=stock_to_sell, order_num=order_num)

SQL 语句似乎没有传递任何内容,它在 HTML 渲染中只是空白。但作为测试,我也使用了“鱼”,它也是无/空的。

神社的样子:

{% block main %}
<list>
    <ul>{{ stock_to_sell }}</ul>
    <ul>{{ order_num }}</ul>
</list>
{% endblock %}

所以页面正文有订单号,但是 stock_to_sell 始终为空。

【问题讨论】:

  • 你正在传递一个列表;这是你的意图吗?如果你只通过'fish' 会发生什么?
  • 是的,jinja 使用 for 和列表更复杂,但我简化了直到我可以让 stock_to_sell 渲染。 'fish' 也不起作用。

标签: python sql python-3.x flask jinja2


【解决方案1】:

您的问题不再与 jinja 有关。你的路线错了,应该是:@app.route("/sellselected/&lt;order_num&gt;", methods=["GET", "POST"])

因为你将 order_num 传递给了 sellselected 函数,所以你需要在路由上声明它。

【讨论】:

【解决方案2】:

您说您喜欢将参数 order_num 作为 GET 传递,对吗?因为代码说 POST 是重定向到索引。

因此,您将参数作为 GET 传递。你需要先得到它

current_order = request.args.get('order_num')

你的代码应该是:

@app.route("/sellselected", methods=["GET", "POST"])
@login_required
def sellselected(order_num):
    if request.method == "POST":
        # not done, just have a redirect to index
    else:
        order_num = request.args.get('order_num')
        stock_to_sell = db.execute("SELECT * FROM purchases WHERE order_num = :order_num", order_num=order_num)

return render_template("sellselected.html", stock_to_sell=stock_to_sell, order_num=order_num)

【讨论】:

    【解决方案3】:

    请注意,如果您迭代一个对象,那么它的内容将返回为空。这是因为结果是一个生成器,它的值只能被访问一次。

    由于 gcp_certs 已被迭代,因此无法工作:

      gcp_certs = connection.execute(query)
      for cert in gcp_certs:
         print(cert.name)
        
      return render_template('certifications.html',
                               gcpcerts=gcp_certs,
                               now=datetime.utcnow(),
                               cookie=token)
    

    作品:

      gcp_certs = connection.execute(query)
      return render_template('certifications.html',
                               gcpcerts=gcp_certs,
                               now=datetime.utcnow(),
                               cookie=token)
    

    一种解决方案:

    imports copy
    
    gcp_certs = list(connection.execute(query))
    sql_select = ''
    for cert in copy.copy(gcp_certs):
         print(cert.name)
    
    return render_template('certifications.html',
                               gcpcerts=gcp_certs,
                               now=datetime.utcnow(),
                               cookie=token)
    

    【讨论】:

      猜你喜欢
      • 2014-12-17
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      • 1970-01-01
      • 1970-01-01
      • 2023-01-11
      相关资源
      最近更新 更多