最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Flask中添加了Flask-Login和login_required后访问某个路由出错:405 Method Not Allowed the method is not allowed for the requested URL

Flask crifan 8893浏览 0评论

之前已经添加了Flask-Login:

[记录]如何在Flask中实现记住微信公众号的用户帐户的登录状态

且给此处的对应的一个路由接口:join_event,添加了login_required:

@app.route(‘/join_event/<int:event_id>’, methods=[‘POST’])
@login_required
def join_event(event_id):
    requestMethod = request.method
    requestArgs = request.args
    gLog.debug(‘requestMethod=%s, requestArgs=%s’, requestMethod, requestArgs)
    gLog.debug(‘type(event_id)=%s, event_id=%s’, type(event_id), event_id)
    eventIdInt = event_id
    curUser = g.user
    gLog.debug(“type(curUser)=%s, curUser=%s”, type(curUser), curUser)

然后内部调试期间,也发现:

已经去实现,跳转到微信授权登录的页面了

但是此处竟然是:

如图(横屏的截图):

搜:

Method Not Allowed the method is not allowed for the requested URL

参考:

python – Flask Error: “Method Not Allowed The method is not allowed for the requested URL” – Stack Overflow

python – The method is not allowed for the requested URL. in Flask – Stack Overflow

python – method not allowed error in flask – Stack Overflow

python – Flask – POST – The method is not allowed for the requested URL – Stack Overflow

python – Method Not Allowed – The method is not allowed for the requested URL – Stack Overflow

->

后来从log中看到:

[2016-09-02 20:25:44,349 DEBUG views.py:919 join_event] type(curUser)=<class ‘werkzeug.local.LocalProxy’>, curUser=<User nickname=u’\u793c\u8c8c’ openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY avatar_static_path=img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png>
[2016-09-02 20:25:44,350 DEBUG views.py:922 join_event] toAddUserOpenid=
[2016-09-02 20:25:44,350 DEBUG views.py:306 joinToEvent] userOpenid=, eventIdInt=6
[2016-09-02 20:25:44,350 DEBUG views.py:228 getUserInfo] curUserOpenid=
[2016-09-02 20:25:44,353 DEBUG views.py:223 getUserFromDb] existedUser=None
[2016-09-02 20:25:44,353 DEBUG views.py:233 getUserInfo] can not get user from db for curUserOpenid=, now try to get user info from wechat API
[2016-09-02 20:25:44,608 ERROR views.py:241 getUserInfo] for  do wechat.get_user_info occur error=40003: invalid openid hint: [PYimla0144vr23]
[2016-09-02 20:25:44,609 DEBUG views.py:244 getUserInfo] type(userInfoDict)=<type ‘dict’>, userInfoDict={}
[2016-09-02 20:25:44,609 DEBUG views.py:251 getUserInfo] foundUser=None
[2016-09-02 20:25:44,610 DEBUG views.py:312 joinToEvent] toAddUser=None
[2016-09-02 20:25:44,610 DEBUG views.py:316 joinToEvent] joinedOk=False, errorStr=找不到该用户
[2016-09-02 20:25:44,611 DEBUG views.py:925 join_event] joinedOk=False, errorStr=找不到该用户
[2016-09-02 20:25:44,773 DEBUG views.py:609 load_user] type(userOpenid)=<type ‘unicode’>, userOpenid=oswjmv4X0cCXcfkIwjoDfCkeTVVY
[2016-09-02 20:25:44,774 DEBUG views.py:228 getUserInfo] curUserOpenid=oswjmv4X0cCXcfkIwjoDfCkeTVVY

所以才知道:

是此处的代码没有写好:

把:

    toAddUserOpenid = request.args.get(“toAddUserOpenid”, “”)
    gLog.debug(“toAddUserOpenid=%s”, toAddUserOpenid)
    (joinedOk, errorStr) = joinToEvent(userOpenid = toAddUserOpenid, eventIdInt = eventIdInt)

改为:

    curUser = g.user
    gLog.debug(“type(curUser)=%s, curUser=%s”, type(curUser), curUser)
    # toAddUserOpenid = request.args.get(“toAddUserOpenid”, “”)
    # gLog.debug(“toAddUserOpenid=%s”, toAddUserOpenid)
    # (joinedOk, errorStr) = joinToEvent(userOpenid = toAddUserOpenid, eventIdInt = eventIdInt)
    (joinedOk, errorStr) = joinToEvent(userOpenid = curUser.openid, eventIdInt = eventIdInt)

即,此处不再用之前的每次把当前的用户的opendi传递来传递去的

而是:

用g.user保存已登录的用户

就可以正常的实现加入活动的功能了。

[总结]

此处是代码写的有问题:

之前已经整合了Flask-Login,且给join_event添加了login_required,但是join_event内部却没有使用g.user,而是获取,已经去掉了没有传递的参数:toAddUserOpenid

导致后续用户找不到,出现405的错误。

后来正常的使用g.user,就可以正常访问页面,正常加入此处的活动了。

转载请注明:在路上 » [已解决]Flask中添加了Flask-Login和login_required后访问某个路由出错:405 Method Not Allowed the method is not allowed for the requested URL

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.298 seconds, using 22.12MB memory