So. I’m creating a login page app. I have a registration page where you can add username, email and password, then you need to confirm your email through a code that’s send into your email
everything works well, including sending the email, but confirming the user code with the template input is giving me problems
first of all, I’ll show my views (won’t waste time with model because it’s a simple user model, with a boolean field called is_email_confirmed with default = False)
views.py
def createuser(request):
form = MyUserCreationForm()
confirmed = User.is_email_confirmed
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
usercode = user.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])
input_code = request.POST.get('verify_code')
# NOT WORKING
if input_code == usercode:
confirmed = User.is_email_confirmed == True
if confirmed:
user.is_active = True
user.save()
return redirect('home')
else:
messages.error(request,'Wrong code.')
else:
messages.error(request,'An error occured during your registration')
context = {'form':form}
return render(request, 'signup.html', context)
as I said, everything seems to work, until here:
input_code = request.POST.get('verify_code')
# NOT WORKING
if input_code == usercode:
confirmed = User.is_email_confirmed == True
if confirmed:
user.is_active = True
user.save()
return redirect('home')
else:
messages.error(request,'Wrong code.')
in my template, I have
{% block content%}
{% if form.is_valid == False %}
<div>
<form method="POST" action="">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Register" />
</form>
<p>Already signed up?</p>
<a href="{% url 'login' %}">Login</a>
</div>
{% else %}
<div>
<form method="POST" action="{% url 'signup' %}">
{% csrf_token %}
Verification Code <input type="text" name="verify_code">
<button type="submit">Submit</button>
</form>
</div>
{% endif %}
{% endblock content %}
showing with prints the error, after digiting anything here (including the real code)…
everything will back to the registration page with this error:
the user is also not active, as you can see in django model:
everything is working but this little piece of code