View returning user as None

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?

Possibly a number of things, not the least of which is relying upon the session for storing the code.

What I suggest you do is read the Django-provided password-reset views to see how they create and use tokens for verifying accounts. You can use this same mechanism here - create and send a token as a url, then when they click on the url, you know that they have access to the identified email address.

it’s so strange because everything is working, even the confirmation code. I’m just not knowing how to get the current user.

Isn’t there anything I can change in username = User.objects.get('username')?

an user in stackoverflow said I should use User.objects.get(username=user_name) but didn’t said what user_name stands for, because its not a variable I have. :confused:

It might be working now, but it’s not going to be robust or reliable in a production environment.

Yes, this is an invalid query.

If you’re not familiar with how Django queries are written, I suggest you work your way through the Official Django Tutorial, especially the first two pages which is where the majority of the tutorial information on writing queries is presented.