0
votes

I doing custom reset password with email confirm.

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.

template:

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>

I send email for change password for not authorisation user(forgot password). He followed link from email. He must input password and confirm and save form. How i can autherisation user? I think, I must used (uidb64, token).

1
Your question is unclear. This code expects a user to already be logged in. - Daniel Roseman
I send email for change password for not authorisation user(forgot password). He followed link from email. He must input password and confirm and save form. - Rustam Pulatov
But then you should not be using request.user. Why are you doing that? The point is that the user is identified by the UID and the token. - Daniel Roseman
Note that in any case this entire code is pointless as this functionality is already provided by Django. There is no need to write it yourself. - Daniel Roseman
Yes. I use functionality from django like this: path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'), It's working, but i want do this without admin panel. What is better way? - Rustam Pulatov

1 Answers

0
votes

It's very yeasy. Thank you, @Daniel Roseman.

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