# email exists while authenticating

**URL:** <https://forum.djangoproject.com/t/email-exists-while-authenticating/5175>\
**Category:** Using Django\
**Created:** [November 11, 2020, 8:37pm UTC](https://forum.djangoproject.com/t/email-exists-while-authenticating/5175 "2020-11-11T20:37:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rabsi12](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@rabsi12](https://forum.djangoproject.com/u/rabsi12)\
**Post date:** [November 11, 2020, 8:37pm UTC](https://forum.djangoproject.com/t/email-exists-while-authenticating/5175/1 "2020-11-11T20:37:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 13, 2020, 2:17am UTC](https://forum.djangoproject.com/t/email-exists-while-authenticating/5175/2 "2020-11-13T02:17:57Z")

</div>

A `clean_` method must return the value to replace that field, so instead of returning redirect, you **must** return the value for the email field.

This would not be the right location to perform the actions you’re trying to perform.
