我認爲應該驗證新的電子郵件地址。如果您的應用程序定期發送電子郵件,您不希望將大量電子郵件發送到用戶輸入的任何電子郵件地址。
我所做的是允許多個電子郵件地址,直到第二封電子郵件被驗證。然後,聽取eljar建議的django-allauth的email_confirmed
信號。驗證地址後,請將新的電子郵件地址設置爲主要電子郵件地址,然後刪除以前的任何EmailAddess
。下面是我最後做一個簡單的例子:
型號:
from django.dispatch import receiver
from allauth.account.models import EmailAddress
from allauth.account.signals import email_confirmed
class CustomUser(models.Model):
...
def add_email_address(self, request, new_email):
# Add a new email address for the user, and send email confirmation.
# Old email will remain the primary until the new one is confirmed.
return EmailAddress.objects.add_email(request, self.user, new_email, confirm=True)
@receiver(email_confirmed)
def update_user_email(sender, request, email_address, **kwargs):
# Once the email address is confirmed, make new email_address primary.
# This also sets user.email to the new email address.
# email_address is an instance of allauth.account.models.EmailAddress
email_address.set_as_primary()
# Get rid of old email addresses
stale_addresses = EmailAddress.objects.filter(
user=email_address.user).exclude(primary=True).delete()
觀點:
def update_user_details(request):
user = request.user
new_email = request.POST.get('new_email')
user.custom_user.add_email_address(request, new_email)
...
看看在['EmailView'](https://github.com /pennersr/django-allauth/blob/master/allauth/account/views.py#L209)和['AddEmailForm'](https://github.com/pennersr/django-allauth/blob/master/allauth/account /forms.py#L277)。 – elssar
謝謝elssar,只是更新了我的問題。 – schacki