2017-03-02 125 views
0

我在制作动态URI的时候需要传递参数。当我尝试传递一个参数时,出现了类型错误消息。它一直说我传递了0个参数。我找不到问题。Flask:TypeError,无法传递参数

这是views.py

@app.route('/delete_todo/<int:todo_id>', methods=['POST']) 
@login_required 
def delete_todo(todo_id): 
    if request.form['todo_id']: 
     g.db.execute('''delete * from todo where todo_id = ?''', request.form['todo_id']) 
     g.db.commit() 
     flash('Your message was deleted') 
    return redirect(url_for('index')) 

范本中应该经过论证

{% for todo in todo %}  
<form action="{{ url_for('delete_todo', todo_id=todo.todo_id) }}" method="post"> 
     <input type="hidden" name="todo_id" value="{{ todo.todo_id }}"> 
     <input type="submit" value="Delete"> 
</form> 
{% else %} 
{% endfor %} 

这是错误消息。

TypeError 
TypeError: _wrapped_view() takes at least 1 argument (0 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) 
TypeError: _wrapped_view() takes at least 1 argument (0 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 
Brought to you by DON'T PANIC, your friendly Werkzeug powered traceback interpreter. 
+0

为什么在使用'POST'方法时在URL中传递参数? – stamaimer

+0

已经可以从request.form ['todo_id']''todo_id''为什么使用动态url? – metmirr

回答

0

todo_id已经通过表单发送,因此您不需要将它作为参数在url中发送。另外不要忘记将字符串todo_id转换为int

@app.route('/delete_todo', methods=['POST']) 
@login_required 
def delete_todo(): 
    if request.form['todo_id']: 
     g.db.execute('''delete * from todo where todo_id = ?''',int(request.form['todo_id'])) 
     g.db.commit() 
     flash('Your message was deleted') 
    return redirect(url_for('index'))