Django不为AnonymousUser提供数据库表示

时间:2019-09-11 12:05:23

标签: django django-forms

我正在通过电子邮件确认进行自定义重置密码。

path('password_reset', views.reset_password_view, name='password-reset'),
path('password_reset/done', views.reset_password_done_view, name='password_reset_done'),
path('reset/<uidb64>/<token>/', views.password_confirm_view, name='password_reset_confirm') #this url

def password_confirm_view(request, uidb64, token):
context = {}
if request.method == 'POST':
    form = SetPasswordForm(data=request.POST, user=request.user)
    if form.is_valid():
        form.save() #I have error
        update_session_auth_hash(request, form.user)
        return redirect('password_reset_done')
else:
    form = SetPasswordForm(user=request.user)
context['form'] = form
return render(request, 'mdm/registration/password_confirm.html', context)



Exception Type:NotImplementedError
Exception Value:Django doesn't provide a DB representation for AnonymousUser.

模板:

form enctype="multipart/form-data" method="post">
                    {% csrf_token %}
                    <div class="input-group register">
                        {{ form.new_password1.errors }}
                        <label for="id_new_password1">Пароль: </label>
                        {{ form.new_password1 }}
                    </div>
                    <div class="input-group register">
                        {{ form.new_password2.errors }}
                        <label for="id_new_password2">Подтвердите пароль: </label>
                        {{ form.new_password2 }}                            
                    </div>                        
                    <div class="justify-content-center">
                        <button class="btn btn-block btn-sm btn-info mt-5" type="submit">Сменить пароль</button>
                    </div>
                </form>

我向非授权用户发送更改密码电子邮件(忘记密码)。他关注了电子邮件中的链接。他必须输入密码并确认并保存表格。 我如何认证用户?我认为,我必须使用(uidb64,令牌)。

1 个答案:

答案 0 :(得分:0)

非常容易。谢谢@Daniel Roseman。

path('reset/<uidb64>/<token>/',
     auth_views.PasswordResetConfirmView.as_view(template_name='app/registration/password_confirm.html'),
     name='password_reset_confirm')