如何在重置密码(PasswordResetView)django中禁用自动发送电子邮件

问题描述 投票:0回答:1

这是我的观点.py

class CustomPasswordResetView(PasswordResetView):
template_name = 'users/password_reset_form.html'

success_url = reverse_lazy('password_reset')

def form_valid(self, form):
    email = form.cleaned_data['email']
    
    # Check if the user with the provided email exists and is active
    try:
        user = User.objects.get(email=email, is_active=True)
    except User.DoesNotExist:
        messages.success(self.request, 'ok.')
        return super().form_valid(form)  # Return the default behavior if user doesn't exist or is inactive
    
    # Generate uidb64 and token
    uidb64 = urlsafe_base64_encode(force_bytes(user.pk))
    token = default_token_generator.make_token(user)
    print(email, uidb64, token)
    messages.success(self.request, 'ok.')
    
            
    return super().form_valid(form)

这是我的 urls.py

urlpatterns = [

path('password_reset/', CustomPasswordResetView.as_view(), name='password_reset'),
path('reset/<uidb64>/<token>/', CustomPasswordResetConfirmView.as_view(), name='password_reset_confirm'),
path('reset/done/', CustomPasswordResetCompleteView.as_view(), name='password_reset_complete'),

]

这是我的 settin.py :

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = '*****'
EMAIL_PORT = 465
EMAIL_USE_SSL = True
EMAIL_HOST_USER = '******'
EMAIL_HOST_PASSWORD = '*****'
AUTH_USER_MODEL = 'users.User' 
ACCOUNT_EMAIL_CONFIRMATION_MODEL = 'users_custom.CustomEmailConfirmation'

当我在 users/password_reset_form.html 中输入重置密码的电子邮件地址时,djnafo 自动发送电子邮件,我可以在控制台中看到它,如下所示:

You're receiving this email because you requested a password reset for your user     account at http://127.0.0.1:8000.

 Please go to the following page and choose a new password:

http://http://127.0.0.1:8000/users/reset/MQ/c0exppa87e20490eb2e3c11513cad273efc4cc/

Your username, in case you’ve forgotten: [email protected]

Thanks for using our site!

The http://127.0.0.1:8000 team

如何禁用此功能?

python django
1个回答
0
投票

如果您想在 Django 中禁用自动电子邮件发送功能,可以使用 EMAIL_BACKEND 设置切换到虚拟后端或不发送电子邮件的替代后端。您当前正在使用控制台后端,它仍然将电子邮件输出到控制台。

要禁用电子邮件发送,您可以使用 django.core.mail.backends.dummy.EmailBackend。更新您的设置.py:

EMAIL_BACKEND = 'django.core.mail.backends.dummy.EmailBackend'

此后端不会发送真正的电子邮件,但会表现得好像确实发送了电子邮件,因此您将不再在控制台中看到电子邮件。

但是如果您只想禁用此功能,

self.email_backend.send_messages([])

将此行添加到 form_valid 函数中。

希望对您有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.