2017-03-03 14 views
-2

私は項目を更新し、このエラーメッセージを受け取るメソッドに取り組んでいます。 私は3つの引数を送信しようとしています.2つの引数はデータベースで更新する必要があり、1つは条件です。しかし、エラーメッセージは4つの引数を渡していると言っています。Flask:TypeError:関数は最大で2つの引数をとります(4が指定されています)

これは、これはテンプレートです

@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')) 

views.pyです。

{% 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')) 
関連する問題