我最初是通过使用django-registration来实现注册电子邮件验证。我已经在我的django应用程序中实现了一个自定义登录/注册表单,以允许用户使用唯一的电子邮件地址登录。但是,这样做会覆盖我的django-registration工作流程。
理想情况下,我想在用户注册后向用户发送验证电子邮件 - 而不是将其重定向到登录页面。我不确定这是否与settings.py文件有关。
models.py
class AccountUserManager(UserManager):
def _create_user(self, username, email, password,
is_staff, is_superuser, **extra_fields):
"""
Creates and saves a User with the given username, email and password.
"""
now = timezone.now()
if not email:
raise ValueError('The given username must be set')
email = self.normalize_email(email)
user = self.model(username=email, email=email,
is_staff=is_staff, is_active=True,
is_superuser=is_superuser,
date_joined=now, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
class User(AbstractUser):
# now that we've abstracted this class we can add any
# number of custom attribute to our user class
# in later units we'll be adding things like payment details!
objects = AccountUserManager()
forms.py
class UserRegistrationForm(UserCreationForm):
email = forms.EmailField(
label='',
widget=forms.EmailInput(attrs={'placeholder': 'Email Address'})
)
password1 = forms.CharField(
label='',
widget=forms.PasswordInput(attrs={'placeholder': 'Password'})
)
password2 = forms.CharField(
label='',
widget=forms.PasswordInput(attrs={'placeholder': 'Confirm Password'})
)
class Meta:
model = User
fields = ['email', 'password1', 'password2']
exclude = ['username']
def clean_password2(self):
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password1 and password2 and password1 != password2:
message = "Passwords do not match"
raise ValidationError(message)
return password2
def save(self, commit=True):
instance = super(UserRegistrationForm, self).save(commit=False)
# automatically set to email address to create a unique identifier
instance.username = instance.email
if commit:
instance.save()
return instance
views.py
def register(request, register_form=UserRegistrationForm):
if request.method == 'POST':
form = register_form(request.POST)
if form.is_valid():
form.save()
user = auth.authenticate(email=request.POST.get('email'),
password=request.POST.get('password1'))
if user:
messages.success(request, "You have successfully registered")
return redirect(reverse('profile'))
else:
messages.error(request, "unable to log you in at this time!")
else:
form = register_form()
args = {'form':form}
args.update(csrf(request))
return render(request, 'register.html', args)
答案 0 :(得分:1)
我不确定你是否还想使用django-registration?
如果您仍想使用django-registration,则会记录HMAC身份验证here。
如果不是,您需要自己发送邮件。 (例如,使用django.core.mail),然后返回渲染的模板或重定向。