so, I’m trying to make a function in my django project that sends a confirmation key through email for the user
so I tried to do this:
views.py
def createuser(request):
form = MyUserCreationForm()
if request.method == 'POST':
form = MyUserCreationForm(request.POST)
if form.is_valid():
return redirect('confirm-email')
else:
messages.error(request,'An error occured during your registration')
context = {'form':form}
return render(request, 'signup.html', context)
def confirmemail(request):
form = MyUserCreationForm()
if request.method == 'POST':
form = MyUserCreationForm(request.POST)
if form.is_valid():
email = form.cleaned_data['email']
user = User.objects.get(email=email)
subject = 'Confirm your email'
from_email = 'myemail@gmail.com'
email = [email]
recipient_list = email
return send_mail(subject, 'Use %s to confirm your email.' % request.user.confirmation_key, from_email, recipient_list)
user.confirm_email(user.confirmation_key)
if user.is_confirmed == True:
user = form.save(commit=False)
user.username = request.user.username.lower()
user.save()
login(request,user)
return redirect('home')
context = {'form':form}
return render(request, 'emailconfirm.html', context)
models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
from simple_email_confirmation.models import SimpleEmailConfirmationUserMixin
class User(SimpleEmailConfirmationUserMixin, AbstractUser):
name = models.CharField(max_length = 50)
username = models.CharField(max_length = 50, null=True)
email = models.EmailField(unique=True, null=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name','username']
I tried to make everything but it didn’t work. I don’t receive any error but also I don’t send any email