-1

So, I'm creating a user confirmation system where a code is created, stored in the session and send through email

I made some prints inside the view that is used to confirm the code and active the user, the code is this one:

def activeThroughCode(request,):
    form = VerificationForm()
    if request.method == 'POST':
        form = VerificationForm(request.POST)
        if form.is_valid:
            entered_code = request.POST.get('verification_code')

            stored_code = request.session.get('verification_code')
            
            print("Entered Code:", entered_code)
            print("Stored Code:", stored_code)

            username = User.objects.get('username')

            if entered_code == stored_code:
                print(username)
                try:
                    user = User.objects.get(username=username)
                    user.is_active = True
                    user.save()
                    messages.success(request, f'Your account is active with success.')
                    return redirect('login')
                except User.DoesNotExist:
                    messages.error(request, 'Incorrect verification code.')
                    return redirect('login')

               
            else:
                messages.error(request, f"It wasn't possible to confirm your email")

    return render(request, 'activate.html',{'form': form})

When digiting the code in my form, I can confirm through terminal the entered code and stored code are the same, and everything is ok until here

but, for validating my user, I need, obviously, request the user to active and save it, but the result for

 username = User.objects.get('username')

            if entered_code == stored_code:
                print(username)

is None.

I'm 99% sure it's the only error, because my Exception is being printed to me, so it means the code confirmation is actually working

my forms:

class   MyUserCreationForm(UserCreationForm):
    class Meta:
        model = User
        fields = ['username','email','password1','password2']

class VerificationForm(forms.Form):
    verification_code = forms.CharField(label='Verification Code', max_length=10)

any idea of what could be wrong?

4
  • 2
    have you checked the type of the values compared ? Commented Mar 17, 2024 at 23:16
  • you mean entered_code == stored_code ? it's all ok. The error is requesting the user. Chat GPT said about using user id for this and some stuff with pk but I didn't understand well how to apply it. I just know I need my activation template to request the user. Commented Mar 17, 2024 at 23:28
  • 1
    I'm more surprised that username = User.objects.get('username') doesn't raise an exception. Commented Mar 17, 2024 at 23:56
  • It only returns None. I can't save the user if I can't get his username, email or whatever. I'm trying something with id but I'm getting error from everywhere Commented Mar 18, 2024 at 0:09

2 Answers 2

0

you have a bug on line 5:

if form.is_valid:

This will always return True, because it just check the method exists. It should be a call of the method instead:

if form.is_valid():
Sign up to request clarification or add additional context in comments.

Comments

0

If you obtain some values from your database. At that time, write this line

model  = User

get username from to database

user  =  User.objects.get(username =username)

in your case

 user = User.objects.get(username)--->> that is wrong

and

form.is_valid():-->write like this

1 Comment

'username' is not defined. that''s what vs code shows me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.