简体   繁体   English

Flask 分页:UnboundLocalError:在赋值错误之前引用了局部变量“res”

[英]Flask Pagination : UnboundLocalError: local variable 'res' referenced before assignment error

I'm working on the flask pagination, when i try to achieve it, i get the UnboundLocalError, if i try to remove all the variables, still the pagination doesn't work.我正在处理 flask 分页,当我尝试实现它时,我得到 UnboundLocalError,如果我尝试删除所有变量,分页仍然不起作用。

@app.route('/')
def index():
    cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor) 
    return render_template('index.html')

@app.route("/fetchrecords",methods=["POST","GET"])
def fetchrecords():
    per_page = 10
    page = request.args.get(get_page_parameter(), type=int, default=1)
    offset = (page - 1) * per_page
    cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
    if request.method == 'POST':
         query = request.form['query']
         cur.execute("SELECT * FROM emp ORDER BY pid")
         res = cur.fetchall()
         for i in res:
             total +=float(i['salary'])
         numrows = int(cur.rowcount)             
         cur.execute("SELECT * FROM emp ORDER BY pid DESC LIMIT %s, %s", (per_page, offset))
         res = cur.fetchall()
         if query == '':
             cur.execute("SELECT * FROM emp ORDER BY pid")
             total = cur.fetchall() 
             numrows = int(cur.rowcount)      
             cur.execute("SELECT * FROM emp ORDER BY pid DESC LIMIT %s, %s", (per_page, offset))
             res = cur.fetchall()     
             pagination = Pagination(page=page, per_page=per_page, offset=offset, total=len(total))                                           
        else:
              search_text = request.form['query']
              cur.execute("SELECT * from emp WHERE name LIKE '%{}%' OR title LIKE '%{}%' OR pid LIKE '%{}%' ORDER BY pid".format(search_text,search_text,search_text))
              res = cur.fetchall() 
              for i in res: 
                  total +=float(i['salary'])
              numrows = int(cur.rowcount) 
              pagination = Pagination(page=page, per_page=per_page, offset=offset, total=len(total), record_name='res')
  elif request.method == 'GET':
        cur.execute("SELECT * FROM emp ORDER BY pid")
        total = cur.fetchall()
        per_page = 25 
        page = request.args.get(get_page_parameter(), type=int, default=1)      
        offset = (page - 1) * per_page
        pagination = Pagination(page=page, per_page=per_page,total=len(total), 
              css_framework='bootstrap4')   
        numrows = int(cur.rowcount)
        cur.execute("SELECT * FROM portal ORDER BY pid DESC LIMIT %s, %s", (per_page, offset))
        res = cur.fetchall()    
             
  return jsonify({'htmlresponse': render_template('response.html', res=res, numrows=numrows, total=total,pagination=pagination)})

resonse.html响应.html

<h4>Total : {{numrows}} </h4></tr>
<table class="table table-striped table-bordered">
    <thead>
        <tr>
          ..Table header.. 
        </tr>
    </thead>
    {% for row in res %}  
    <tr>  Data base values here...</tr>
    {% endfor %}
</table>
{{pagination.links}}

Can anyone please help me, how to work with pagination and whats wrong I'm doing.任何人都可以帮助我,如何使用分页以及我在做什么错。 This would be very helpful thanks in advance这将非常有帮助 提前致谢

It happens when the endpoint is invoked via an http get request.当通过http get请求调用端点时会发生这种情况。 The creation of the res variable is enclosed within this if: res变量的创建包含在以下情况下:

if request.method == 'POST':

while this return here (which refers to the variable res ) is outside the if而这里的return (指的是变量res )在 if 之外

return jsonify({'htmlresponse': render_template('response.html', res=res, numrows=numrows, total=total,pagination=pagination)})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnboundLocalError:赋值前引用的局部变量'error' - UnboundLocalError: local variable 'error' referenced before assignment UnboundLocalError: 赋值前引用的局部变量错误 - UnboundLocalError: local variable referenced before assignment error Flask 日期时间:UnboundLocalError:分配前引用的局部变量“日期” - Flask Datetime: UnboundLocalError: local variable 'date' referenced before assignment 烧瓶 UnboundLocalError:在赋值之前引用了局部变量“shift1” - flask UnboundLocalError: local variable 'shift1' referenced before assignment UnboundLocalError:在分配Flask应用之前引用了本地变量&#39;restaurantToDelete&#39; - UnboundLocalError: local variable 'restaurantToDelete' referenced before assignment Flask app Flask UnboundLocalError:赋值前引用了局部变量“latitu” - Flask UnboundLocalError: local variable 'latitu' referenced before assignment UnboundLocalError:在 Flask 中赋值之前引用了局部变量“dna” - UnboundLocalError: local variable 'dna' referenced before assignment in Flask UnboundLocalError:分配前已引用局部变量“ item”(python,flask) - UnboundLocalError: local variable 'item' referenced before assignment (python, flask) Python Flask重定向-UnboundLocalError:分配错误之前引用了局部变量 - Python Flask redirect- UnboundLocalError: local variable referenced before assignment error Flask 引发的间歇性错误:UnboundLocalError:分配前引用的局部变量“resp” - Intermittent error thrown by Flask: UnboundLocalError: local variable 'resp' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM