【问题标题】:TypeError: __init__() got an unexpected keyword argument 'method'TypeError: __init__() 得到了一个意外的关键字参数“方法”
【发布时间】:2017-06-26 21:02:25
【问题描述】:

好吧,我正在写一个 Flask 项目,但是当我尝试 python manage.py

回溯告诉我:

Traceback (most recent call last):
  File "manage.py", line 5, in <module>
    from app import db,create_app
  File "/home/humbert/2017-sharing-backend/sharing/app/__init__.py", line 42, in <module>
    app.register_blueprint(main_blueprint, url_prefix='/main')
  File "/home/humbert/venv/local/lib/python2.7/site-packages/flask/app.py", line 64, in wrapper_func
    return f(self, *args, **kwargs)
  File "/home/humbert/venv/local/lib/python2.7/site-packages/flask/app.py", line 951, in register_blueprint
    blueprint.register(self, options, first_registration)
  File "/home/humbert/venv/local/lib/python2.7/site-packages/flask/blueprints.py", line 154, in register
    deferred(state)
  File "/home/humbert/venv/local/lib/python2.7/site-packages/flask/blueprints.py", line 173, in <lambda>
    s.add_url_rule(rule, endpoint, view_func, **options))
  File "/home/humbert/venv/local/lib/python2.7/site-packages/flask/blueprints.py", line 76, in add_url_rule
    view_func, defaults=defaults, **options)
  File "/home/humbert/venv/local/lib/python2.7/site-packages/flask/app.py", line 64, in wrapper_func
    return f(self, *args, **kwargs)
  File "/home/humbert/venv/local/lib/python2.7/site-packages/flask/app.py", line 1043, in add_url_rule
    rule = self.url_rule_class(rule, methods=methods, **options)
TypeError: __init__() got an unexpected keyword argument 'method'

我认为我的manage.py 是对的,我想不出错误。

__init__.py的错误部分是:

from .main import main as main_blueprint
app.register_blueprint(main_blueprint, url_prefix='/main')

from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint, url_prefix="/auth")

from . import views

我真的需要一些帮助,谢谢!

【问题讨论】:

  • 如果你的代码中还有一些东西,你必须调试你的代码。就我而言,我错过了一个必须添加的id 属性。

标签: python flask


【解决方案1】:

我遇到了类似的问题。在我的代码中,我有一行 @bp.route('/<init:id>/delete', method=('POST')) 关键字method需要改为methods(with an s)

【讨论】:

  • 我的 todo 烧瓶项目在尝试处理时也遇到了同样的问题:@app.route('/todos/create', method=['POST']) 你的回答可以帮助我解决通过添加 's' 来解决问题:@app.route('/todos/create', methods=['POST']) 感谢 Nicholas Harder。
  • 当我可以查看较旧的 SO 帖子并得到正确答案时总是很好,就像这样......愚蠢的错别字,而且 pylint 在 Flask 中并没有捕捉到很多东西,因为它是。 ..
【解决方案2】:

我在我的代码中遇到了类似的问题,只需要将关键字“method”更改为“methods”:

@app.route('/login', methods = ['GET', 'POST'])

【讨论】:

  • 近五个月后,您刚刚重复了接受的答案!为什么?哈哈
猜你喜欢
  • 2012-12-11
  • 1970-01-01
  • 2017-10-16
  • 2020-10-22
  • 2021-11-17
  • 2016-11-01
  • 2021-10-21
  • 2021-10-15
  • 2021-12-30
相关资源
最近更新 更多