# View returning user as None

**URL:** https://forum.djangoproject.com/t/view-returning-user-as-none/29196
**Category:** Forms & APIs
**Created:** [March 17, 2024, 11:16pm UTC](https://forum.djangoproject.com/t/view-returning-user-as-none/29196 "2024-03-17T23:16:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![taborda17](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/taborda17/32/18401_2.png) [@taborda17](https://forum.djangoproject.com/u/taborda17)
#### Post date: [March 17, 2024, 11:16pm UTC](https://forum.djangoproject.com/t/view-returning-user-as-none/29196/1 "2024-03-17T23:16:53Z")

</div>

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:

```auto
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

```auto
 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:

```auto
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?

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [March 18, 2024, 12:47am UTC](https://forum.djangoproject.com/t/view-returning-user-as-none/29196/2 "2024-03-18T00:47:37Z")

</div>

> [@taborda17](#):
>
> 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.

---

<div class="post-metadata">

### Author: ![taborda17](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/taborda17/32/18401_2.png) [@taborda17](https://forum.djangoproject.com/u/taborda17)
#### Post date: [March 18, 2024, 12:56am UTC](https://forum.djangoproject.com/t/view-returning-user-as-none/29196/3 "2024-03-18T00:56:19Z")

</div>

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. 😕

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [March 18, 2024, 2:00am UTC](https://forum.djangoproject.com/t/view-returning-user-as-none/29196/4 "2024-03-18T02:00:54Z")

</div>

> [@taborda17](#):
>
> it’s so strange because everything is working

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

> [@taborda17](#):
>
> Isn’t there anything I can change in `username = User.objects.get('username')`?

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](https://docs.djangoproject.com/en/4.2/intro/tutorial01/), especially the first two pages which is where the majority of the tutorial information on writing queries is presented.
