【问题标题】:django 1.9 how to keep session after change password through ajax requestdjango 1.9 如何在通过 ajax 请求更改密码后保持会话
【发布时间】:2019-07-12 16:21:47
【问题描述】:

我有一个通过 ajax 更新用户数据的功能

def ajax_update(request):
    if request.method == 'POST':
    update_form = forms.UserUpdateForm(request.POST, instance=request.user)
    if update_form.is_valid():
        update_form.save()
        response['status'] = 'success'
        return JsonResponse(response)

还有一个 UserUpdateForm

class UserUpdateForm(forms.ModelForm):

    class Meta:
        model = User
        exclude = ['password']

    def save(self, commit=True):
        instance = super(UserUpdateForm, self).save(commit=False)
        instance.set_password(self.cleaned_data['password_1'])
        instance.save()
        return instance

set_password 执行并保存实例后,返回“成功”消息。

但是,当我重新加载页面时,它的会话已过期

有没有办法在通过ajax更改密码后保持会话?

【问题讨论】:

    标签: django session authentication passwords


    【解决方案1】:

    这是documented here。您应该使用update_session_auth_hash() 函数。

    但在这种情况下,您还应该更新 javascript 中的会话 cookie,因为会生成新的哈希值。

    【讨论】:

    • 这就是我想要的!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 2015-04-20
    • 2012-03-19
    • 2011-04-20
    • 1970-01-01
    • 2023-03-23
    • 2021-07-31
    相关资源
    最近更新 更多