1
votes

I have a list of users in a ListView CBV and want to add a "reset password" link that will send an email with a reset password token to that user with one click (as the auth_views are currently set up to after you submit a request via a form).

For example:

<td>{{ user.email }}</td>
<td><a href="{% url 'reset_link' user.pk %}">Reset password</a></td>

I am using the CBV auth_views so know I could reset this user's password manually via redirecting to a form and using Django's inbuilt auth_views.PasswordResetView. However as I already have the user.pk I just want a link that instantly sends the relevant email.

Is there a way to send the email using user.pk via a single link if I already know the pk?

1

1 Answers

1
votes

A quick hack can be:

urls.py:

path('<int:user_pk>/my_password_reset/', views.MyPasswordResetView.as_view(), name='my_password_reset'),

views.py:

class MyPasswordResetView(PasswordResetView):
    def get_form_kwargs(self):
        user = get_object_or_404(User, pk=self.kwargs.get('user_pk'))
        return {'data': {'email': user.email}}

    def get(self, request, *args, **kwargs):
        return self.post(request, *args, **kwargs)