Flask:TypeError:函数最多需要2个参数(给定4个)

时间:2017-03-03 01:27:55

标签: python web flask arguments

我正在研究更新项目的方法,现在收到此错误消息。 我试图发送三个参数,需要在数据库中更新两个参数,一个是条件。但是错误信息一直在说我通过了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

由DON&#39; T PANIC带给您,这是一位友善的Werkzeug动力追溯翻译。

1 个答案:

答案 0 :(得分:4)

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