Hi,
when authenticating a user in django, i have the following view:
def register(request):
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
email = form.cleaned_data.get('email')
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, email=email, password=raw_password)
current_site = get_current_site(request)
mail_subject = 'Activate your account.'
message = render_to_string('activation.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': default_token_generator.make_token(user),
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(mail_subject, message, to=[to_email])
email.send(fail_silently=False)
messages.info(request, 'Check ' + user.email + ' inbox to confirm membership')
return redirect('home')
else:
form = RegisterForm()
return render(request, 'register.html', {'form': form})
in the forms i was working on email validation if email exists as such:
def clean_email(self):
email = self.cleaned_data.get('email')
try:
match = User.objects.get(email=email)
except User.DoesNotExist:
return email
raise forms.ValidationError('Email address already registered.')
Instead of raising a validation error i want to send an email like in views
messages.info(request, ‘Check ’ + user.email + ’ inbox to confirm membership’)
so i tried the following and got in loads of trouble, trimmed alot from the original to avoid errors and got this:
def clean_email(self):
email = self.cleaned_data.get('email')
username = self.cleaned_data.get('username')
email = self.cleaned_data.get('email')
raw_password = self.cleaned_data.get('password1')
user = authenticate(username=username, email=email, password=raw_password)
try:
match = User.objects.get(email=email)
except User.DoesNotExist:
return email
mail_subject = 'Request membership'
message = render_to_string('emailexist.html')
to_email = self.cleaned_data.get('email')
email = EmailMessage(mail_subject, message, to=[to_email])
email.send(fail_silently=False)
messages.info('Check ' + user.email + ' inbox to confirm membership')
return redirect('home')
message not working since cant get user.email and if i remove the whole message part i got theis error: ‘HttpResponseRedirect’ object has no attribute ‘strip’
Could i get some support on this, as it seems i strayed far from the right path
Thank you