Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试使用Flask Security手动确认用户电子邮件时,确认令牌无效_Python_Flask_Token_Flask Login_Flask Security - Fatal编程技术网

Python 尝试使用Flask Security手动确认用户电子邮件时,确认令牌无效

Python 尝试使用Flask Security手动确认用户电子邮件时,确认令牌无效,python,flask,token,flask-login,flask-security,Python,Flask,Token,Flask Login,Flask Security,试图找出调用send_confirmation_指令和使用内置的resend confirmation instructions表单调用它之间的区别 以下是我的新用户视图中的调用: newuser = User( email = newform.email.data, first_name = newform.first_name.data, last_name = newform.last_name.data,

试图找出调用send_confirmation_指令和使用内置的resend confirmation instructions表单调用它之间的区别

以下是我的新用户视图中的调用:

newuser = User(
            email = newform.email.data,
            first_name = newform.first_name.data,
            last_name = newform.last_name.data,
            business_name = newform.business_name.data,
            roles = [Role.query.filter_by(id=role_id).first() for role_id in newform.roles.data],
            active = True
            )

        if newform.req_conf.data:
            send_confirmation_instructions(newuser)
        else:
            newuser.confirmed_at = datetime.utcnow()
下面是来自发送确认视图的内置调用:

def send_confirmation():
    """View function which sends confirmation instructions."""

    form_class = _security.send_confirmation_form

    if request.json:
        form = form_class(MultiDict(request.json))
    else:
        form = form_class()

    if form.validate_on_submit():
        send_confirmation_instructions(form.user)
        if request.json is None:
            do_flash(*get_message('CONFIRMATION_REQUEST', email=form.user.email))

    if request.json:
        return _render_json(form)

    return _security.render_template(config_value('SEND_CONFIRMATION_TEMPLATE'),
                                     send_confirmation_form=form,
                                     **_ctx('send_confirmation'))
据我所知,所有的魔法都发生在函数中: 发送确认指示(用户)

我能区分的唯一区别是我如何调用它和Flask Security如何调用它,我使用的是一个用户实例,而内置函数使用form.user

跟踪它使用的表单,我甚至看不到form.user的分配位置:

class SendConfirmationForm(Form, UserEmailFormMixin):

    submit = SubmitField(get_form_field_label('send_confirmation'))

    def __init__(self, *args, **kwargs):
        super(SendConfirmationForm, self).__init__(*args, **kwargs)
        if request.method == 'GET':
            self.email.data = request.args.get('email', None)

    def validate(self):
        if not super(SendConfirmationForm, self).validate():
            return False
        if self.user.confirmed_at is not None:
            self.email.errors.append(get_message('ALREADY_CONFIRMED')[0])
            return False
        return True

我现在完全迷路了,所以我向你求助。知道我做错了什么吗?

决定尝试提交到数据库,然后发送确认,这确实有效。不知道为什么会有不同,因为据我所知,没有任何函数使用或更改数据库中的任何内容,但显然我遗漏了一些东西

更改代码如下,现在一切都好了

    if newform.req_conf.data:
        db.session.add(newuser)
        db.session.commit()
        send_confirmation_instructions(newuser)
    else:
        newuser.confirmed_at = datetime.utcnow()
        db.session.add(newuser)
        db.session.commit()

非常有用。谢谢。四年过去了,这个问题仍然在帮助人们。谢谢你的巧克力糖!