You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask重定向跳转传参报jinja2 UndefinedError错误如何解决

问题原因&解决方法

你遇到的问题核心有两个错误:

  • 第一,url_for()的使用错误:Flask的url_for接收的参数是视图函数名,不是路由路径,你写的url_for('/edit_product.html')是错误写法,应该写视图函数的名称edit_product。
  • 第二,redirect重定向属于客户端跳转,你在url_for里传的参数会以查询参数的形式拼接在跳转后的URL后面,不能直接传递给目标路由对应的模板,需要你在目标路由里主动接收参数后再传递给jinja2模板。

修复步骤

第一步:修改actualize_product路由的跳转代码

同时修正你原有SQL拼接的SQL注入漏洞,替换为参数化查询:

@app.route('/actualize_product.html', methods=['GET', 'POST'])
def actualize_product():
    if request.method == 'POST':
        print("post")
        product_id = request.form['product_id']
        # 参数化查询避免SQL注入
        query1 = """
            SELECT id FROM BD.producto
            WHERE id = %s
        """
        conection = connect()
        # 按你使用的数据库驱动传递参数即可,这里以%s占位符为例
        resultt = conection.execute(query1, (product_id,))[0]
        print(resultt)
        # url_for传视图函数名,只传递主键id即可,不需要传递整个对象
        return redirect(url_for('edit_product', product_id = resultt.id)) 

    query = "SELECT * FROM BD.Producto ALLOW FILTERING; "
    conection = connect()
    result = conection.execute(query)
    return render_template('actualize_product.html', products = result)

注意:不要直接传递整个resultt对象作为URL参数,对象无法被序列化到URL中,只需要传递id主键,到目标路由再查一次完整数据即可,这也是web开发的标准做法。

第二步:修改edit_product路由,接收参数查询数据后传给模板

@app.route('/edit_product.html', methods=['GET', 'POST'])
def edit_product():
    print("edit")
    # 接收跳转传递的product_id参数
    product_id = request.args.get('product_id')
    # 根据id查询完整的产品数据
    query = """
        SELECT * FROM BD.producto
        WHERE id = %s
    """
    conection = connect()
    resultado = conection.execute(query, (product_id,))[0]
    if request.method == 'POST':
        # 这里正常处理你的编辑提交逻辑即可
        print("Im already here")
    # 把查询到的resultado传递给模板
    return render_template('edit_product.html', resultado=resultado)

第三步:修正edit_product.html的form配置

你原有form的action和method配置都存在错误,修正如下:

<h2>Id del producto: {{resultado.id}} </h2> <br>
<!-- action替换为正确的路由地址,提交方法改为POST更符合编辑场景的规范 -->
<form action="{{ url_for('edit_product') }}" method="post" onsubmit="return formEsValido()">
  <input type= "hidden" value = "{{resultado.id}}" id = "id" name = "id">
  <label for="product_name"> Nuevo nombre del Producto: </label>
  <input type="text" name="product_name" id="product_name" class="form-control">
  <label for="product_price"> Nuevo precio del Producto: </label>
  <input type="text" name="product_price" id="product_price" class="form-control">
  <label for="descripction"> Nueva descripcion del Producto: </label>
  <input type="text" name="description" id="description" class="form-control">
  <label for="stock">Nuevo stock del Producto</label>
  <input type="text" name="stock" id="stock" class="form-control">
  <br>  
  <button class="btn btn-primary"id="index-buttons" type="submit">Editar Producto</button>              
</form>
</div>

内容的提问来源于stack exchange,提问作者RodrigoSantisteban

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 23:30:00