2017-03-03 58 views
-2

我正在处理更新项目的方法,现在正在获取此错误消息。 我想发送三个参数,两个参数需要在数据库中更新,一个是条件。然而,错误消息一直说我传递了4个参数。Flask:TypeError:函数至多需要2个参数(给出4个)

这是views.py

@app.route('/update_todo', methods=['POST']) 
def update_todo(): 
    if request.method == 'POST': 
     todo_id = int(request.form['todo_id']) 
     g.db.execute('''update todo set text=?, pub_date=? 
         where todo_id = ?''', request.form['text'], 
              int(time.time()), 
              [todo_id]) 
     g.db.commit() 
     flash('Your message is updated') 
    return redirect(url_for('index')) 

这是模板。

{% extends "layout.html" %} 
{% block body %} 
<div class="container"> 
<div class=twitbox> 
    <h3>Edit message of {{ g.user.username }}?</h3> 
    </div> 
    <ul class=messages> 
    {% for todo in todo %} 
    <li> 
    <form action="{{ url_for('update_todo') }}" method=post> 
    {{ todo.todo_id }} 
    <p><input type=hidden value="{{ todo.todo_id }}" name=todo_id> 
    <input type=text value="{{ todo.text }}" name=text> 
    <input type=submit value="Update"> 
    </form> 
    {% else %} 
    <li><em>None</em> 
    {% endfor %} 
    </ul> 
</div> 
{% endblock %} 

编辑:添加错误信息

TypeError 
TypeError: function takes at most 2 arguments (4 given) 

Traceback (most recent call last) 
File "C:\Python27\lib\site-packages\flask\app.py", line 1994, in __call__ 
return self.wsgi_app(environ, start_response) 
File "C:\Python27\lib\site-packages\flask\app.py", line 1985, in wsgi_app 
response = self.handle_exception(e) 
File "C:\Python27\lib\site-packages\flask\app.py", line 1540, in handle_exception 
reraise(exc_type, exc_value, tb) 
File "C:\Python27\lib\site-packages\flask\app.py", line 1982, in wsgi_app 
response = self.full_dispatch_request() 
File "C:\Python27\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request 
rv = self.handle_user_exception(e) 
File "C:\Python27\lib\site-packages\flask\app.py", line 1517, in handle_user_exception 
reraise(exc_type, exc_value, tb) 
File "C:\Python27\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request 
rv = self.dispatch_request() 
File "C:\Python27\lib\site-packages\flask\app.py", line 1598, in dispatch_request 
return self.view_functions[rule.endpoint](**req.view_args) 
File "C:\Users\ds\workspace\myflask\myflask\todo.py", line 182, in update_todo 
[todo_id]) 
TypeError: function takes at most 2 arguments (4 given) 
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error. 
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side. 

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection: 

dump() shows all variables in the frame 
dump(obj) dumps all that's known about the object 

带给您的是不要惊慌,你的友好WERKZEUG供电回溯解释。

+1

请包括完整的追溯。见[mcve]。 – davidism

+0

另外,为什么'todo_id'包装在'execute'列表中? – davidism

+0

请给[mcve]! – Julien

回答

3

g.db.execute方法需要一个sql和一个参数数组来绑定占位符。修复如下:

@app.route('/update_todo', methods=['POST']) 
def update_todo(): 
    if request.method == 'POST': 
     todo_id = int(request.form['todo_id']) 
     g.db.execute('''update todo set text=?, pub_date=? 
         where todo_id = ?''', [request.form['text'], 
              int(time.time()), 
              todo_id]) 
     g.db.commit() 
     flash('Your message is updated') 
    return redirect(url_for('index')) 
相关问题