【问题标题】:TypeError: remove() takes exactly 1 argument (2 given)类型错误:remove() 只需要 1 个参数(给定 2 个)
【发布时间】:2017-11-22 10:58:40
【问题描述】:

我正在编写代码来删除给定 id 的页面

@app.route("/remove/<int:id>",methods=['POST','GET'])
@login_required
def remove(id):
    PAGE=MY_DAIRY.query.get(id)
    db.session.remove(PAGE)
    db.session.commit()
    flash("done deleting ")
    return render_template("ok.html")

当我通过执行类似“localhost/remove/1”的操作将参数传递给 url 时,我收到以下错误 类型错误

TypeError: remove() takes exactly 1 argument (2 given)

我真的不明白这里有什么问题我只指定了一个参数,我正在传递它。

这是我的 requirements.txt 文件:

Flask==0.12.2 ,Flask-Bootstrap==3.0.3.1, Flask-SQLAlchemy==1.0
Flask-WTF==0.9.4 ,Jinja2==2.7.1, SQLAlchemy==0.8.4 ,WTForms==1.0.5
Werkzeug==0.9.6, Flask-Login==0.4.0

整个回溯:

    File "/home/nidhal/.local/lib/python2.7/site-packages/flask/app.py", line 1997, in __call__

    return self.wsgi_app(environ, start_response)

    File "/home/nidhal/.local/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app

    response = self.handle_exception(e)

    File "/home/nidhal/.local/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception

    reraise(exc_type, exc_value, tb)

    File "/home/nidhal/.local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app

    response = self.full_dispatch_request()

    File "/home/nidhal/.local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request

    rv = self.handle_user_exception(e)

    File "/home/nidhal/.local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception

    reraise(exc_type, exc_value, tb)

    File "/home/nidhal/.local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request

    rv = self.dispatch_request()

    File "/home/nidhal/.local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request

    return self.view_functions[rule.endpoint](**req.view_args)

    File "/home/nidhal/.local/lib/python2.7/site-packages/flask_login/utils.py", line 228, in decorated_view

    return func(*args, **kwargs)

    File "/home/nidhal/Bureau/diaryy/app.py", line 144, in remove

    db.session.remove(PAGE)

    TypeError: remove() takes exactly 1 argument (2 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 ob

项目

【问题讨论】:

  • 您确定错误是由于路由功能而不是由于会话删除功能。你能发布堆栈跟踪而不是一个线性错误
  • 欢迎来到 Stack Overflow!您能否edit 向我们展示此函数的调用 问题。事实上,我们确实需要一个minimal reproducible example,否则这个问题很可能会被关闭为题外话。您能否准确确认是哪一行引发了错误(是调用此函数还是调用db.session.remove?)
  • 我认为你把自己留在了争论中
  • 不,这是对函数 db.session 的调用,删除工作正常
  • 什么是db?你使用的是 pymongo 吗?

标签: python python-2.7 flask


【解决方案1】:

您的错误是由于 sqlachemy 会话删除函数调用造成的。

文件“/home/nidhal/Bureau/diaryy/app.py”,第 144 行,在删除中

db.session.remove(PAGE)

TypeError: remove() takes exactly 1 argument (2 given)

浏览文档here

您想要使用的可能是删除/刷新/关闭。 remove 不带任何参数

【讨论】:

  • 谢谢,我认为这是由于名为 remove 的路由
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-11
  • 1970-01-01
  • 1970-01-01
  • 2014-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多