0
私は当初django登録を使用して登録電子メールの検証を実装し始めました。私は独自の電子メールアドレスでログインできるように、カスタムログイン/登録フォームを私のdjangoアプリケーションに実装しました。しかし、そのようにすると、私のジャンゴ登録ワークフローを覆してしまいます。Django登録フォーム確認メール
理想的には、ログインページにリダイレクトするのではなく、ユーザー登録後にユーザーに確認メールを送信したいと考えています。私はこれが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)
まだそのパッケージから登録確認を引き出すことができるかどうかは確かではありませんでした。 – Nick
まあ、私はdjango登録を意図どおりに使用することをお勧めします(あなたの現在のコードは私が知る限りでは使用しません)。 –