Why is my views.py not working as I pretend?

So, i’m trying to create a user registration project with email confirmation

the user will need to confirm its email first, then be able to login

in settings.py I have:

DEBUG = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'my email here'
EMAIL_HOST_PASSWORD = 'my pw here'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

and in views.py

def createuser(request):
    form = MyUserCreationForm()

    if request.method == 'POST':
        form = MyUserCreationForm(request.POST)
        if form.is_valid():     
            code = User.objects.make_random_password(length=6,allowed_chars='1234567890')

            user = form.save(commit=False)
            user.is_active = False 
            user.save()
            # if User.is_email_confirmed == False:

            user.code = code
            user.save()
            
            subject = 'Confirm your email' 
            confirmation_code = code
            message = f'Confirm your email with this code: {confirmation_code}'
            from_email = '...'
            to_email = request.user.email
            email = EmailMessage(subject, message, to=[to_email])
            email.send()

            # else:
            #     user = form.save(commit=False)
            #     user.username = user.username.lower()
            #     user.save()
            #     login(request,user)
            #     return redirect('home')
        else:
            messages.error(request,'An error occured during your registration')
    context = {'form':form}
    return render(request, 'signup.html', context)

The user is being saved but absolutely nothing is send in any email

then I tried


email = EmailMessage(subject, message, to=[to_email])
email.send()

instead of


send_mail(subject,message,from_email,[to_email])
            return redirect('emailconfirm.html')

nothing solved. Nothinf returns an error exactly but only saves the user and do not send any email

Unless you’ve edited some of this out:

The request.user is not the user that you have just saved. It’s the person who has logged in to fill out this form - which could be anonymous, or someone else.

Also, you should have a way of testing your ability to send emails. Django provides the sendtestemail management command that you can use to verify that everything is configured correctly.

2 Likes

So

my model here:

class User(AbstractUser):
    name = models.CharField(max_length = 50)
    username = models.CharField(max_length = 50, null=True)
    email = models.EmailField(unique=True, null=True)
    is_email_confirmed = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['name','username']

I tried to_email = User.email and I got SMTPRecipientsRefused at /signup/ – send_mail(subject,message,from_email,[to_email])

What do you think I should do to get the saved user email?

Please show the actual code of the view that you’re trying, along with the complete error and traceback.

1 Like

here you go: `def createuser(request):
form = MyUserCreationForm()

if request.method == 'POST':
    form = MyUserCreationForm(request.POST)
    if form.is_valid():     
        code = User.objects.make_random_password(length=6,allowed_chars='1234567890')

        user = form.save(commit=False)
        user.is_active = False 
        user.save()
        

        user.code = code
        user.save()
        
        subject = 'Confirm your email' 
        confirmation_code = code
        message = f'Confirm your email with this code: {confirmation_code}'
        from_email = 'adryanftaborda@gmail.com'
        to_email = User.email
        send_mail(subject,message,from_email,[to_email])
        return redirect('emailconfirm.html')

    else:
        messages.error(request,'An error occured during your registration')
context = {'form':form}
return render(request, 'signup.html', context)`
1 Like

That’s the model - you’re trying to reference the field of a model rather than the email field of an instance of that model.

I think what you’re wanting to use there is probably user.email. In this case user is the instane of the newly saved model.

2 Likes