2017-03-03 233 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')) 
相關問題