# Project does not recognize field

**URL:** https://forum.djangoproject.com/t/project-does-not-recognize-field/28530
**Category:** Forms & APIs
**Created:** [February 28, 2024, 11:11pm UTC](https://forum.djangoproject.com/t/project-does-not-recognize-field/28530 "2024-02-28T23:11:56Z")
**Posts on this page:** 15
**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: [February 28, 2024, 11:11pm UTC](https://forum.djangoproject.com/t/project-does-not-recognize-field/28530/1 "2024-02-28T23:11:56Z")

</div>

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

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

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

```auto
{% 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)…

![Captura de tela 2024-02-28 200830](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/c/2/c20738f6f46cfbb9820dca050b8dd6a31b4723e1.png)

everything will back to the registration page with this error:

 ![Captura de tela 2024-02-28 200926](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/4/1/41c8286c34714f3c2f3ee54ebe0bced3deb066b1.png)

the user is also not active, as you can see in django model:

 ![Captura de tela 2024-02-28 201012](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/1/e/1ed360e099550e5766b76dbe7a9ea8736a3ab4c8.png)

everything is working but this little piece of code

---

<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: [February 29, 2024, 12:00am UTC](https://forum.djangoproject.com/t/project-does-not-recognize-field/28530/2 "2024-02-29T00:00:43Z")

</div>

Each time you execute this block of code in the `if form.is_valid():`, you’re generating a new code.

```auto
        if form.is_valid():     

            code = User.objects.make_random_password(length=6,allowed_chars='1234567890')

      ...
            user.code = code
            usercode = user.code
            user.save()
        ...            
            input_code = request.POST.get('verify_code')
            # NOT WORKING
            if input_code == usercode:
                confirmed = User.is_email_confirmed == True

```

Whatever code was entered in the `verify_code` field is not going to be the same value that was generated when the MyUserCreationForm was submitted.

You need to rework your logic for this process - you’d be better off separating this functionality into separate views.

---

<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: [February 29, 2024, 12:51am UTC](https://forum.djangoproject.com/t/project-does-not-recognize-field/28530/3 "2024-02-29T00:51:06Z")

</div>

> [@KenWhitesell](#):
>
> You need to rework your logic for this process - you’d be better off separating this functionality into separate views.

so I could make  
`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 another view, right? Is that what you’re saying?

---

<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: [February 29, 2024, 1:06am UTC](https://forum.djangoproject.com/t/project-does-not-recognize-field/28530/4 "2024-02-29T01:06:22Z")

</div>

Essentially, yes.

One view to create the user and send the confirmation code.

A different view to enter the code for confirmation.

---

<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: [February 29, 2024, 10:29pm UTC](https://forum.djangoproject.com/t/project-does-not-recognize-field/28530/5 "2024-02-29T22:29:33Z")

</div>

Excuse me, seems kinda late, but

aren’t django views independent? can I share infos of a view with another view? could cls help me in this case?

---

<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: [February 29, 2024, 10:41pm UTC](https://forum.djangoproject.com/t/project-does-not-recognize-field/28530/6 "2024-02-29T22:41:33Z")

</div>

> [@taborda17](#):
>
> aren’t django views independent?

Precisely.

You have two different functions, performing two different operations. Therefore, you should have two separate views.

---

<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: [February 29, 2024, 11:47pm UTC](https://forum.djangoproject.com/t/project-does-not-recognize-field/28530/7 "2024-02-29T23:47:57Z")

</div>

I’m asking how I will reference ` usercode = user.code` in an other view (or any other variable)

---

<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: [February 29, 2024, 11:49pm UTC](https://forum.djangoproject.com/t/project-does-not-recognize-field/28530/8 "2024-02-29T23:49:41Z")

</div>

Aren’t you saving it in the user object? If so, then that’s where you retrieve it from.

---

<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 1, 2024, 12:10am UTC](https://forum.djangoproject.com/t/project-does-not-recognize-field/28530/9 "2024-03-01T00:10:17Z")

</div>

Well, I made the following view

```auto
def verifycode(request):
    user = User.objects.all()
    input_code = request.POST.get('verify_code')
    
    if input_code == user.code:
        user.is_active = True
        user.save()
        return redirect('home')
    else:
        messages.error(request,'Wrong code.')
    return render(request, 'signup.html')

```

and the same problem returned.

OBS: This is my form

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

```

---

<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 1, 2024, 12:22am UTC](https://forum.djangoproject.com/t/project-does-not-recognize-field/28530/10 "2024-03-01T00:22:09Z")

</div>

> [@taborda17](#):
>
> `user = User.objects.all()`

That’s retrieving all users as a queryset, not one specific user that you’re trying to test. That makes this:

> [@taborda17](#):
>
> `if input_code == user.code:`

a syntactically invalid statement.

Which user are you trying to compare this code for? That’s the user you need to retrieve from the User model.

---

<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 1, 2024, 12:24am UTC](https://forum.djangoproject.com/t/project-does-not-recognize-field/28530/11 "2024-03-01T00:24:59Z")

</div>

> [@KenWhitesell](#):
>
> Which user are you trying to compare this code for?

The one that just created a user

if john\_carl created a user with the email = [johncarl123@gmail.com](mailto:johncarl123@gmail.com), then this user should receive an email and then confirm its email

---

<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 1, 2024, 12:27am UTC](https://forum.djangoproject.com/t/project-does-not-recognize-field/28530/12 "2024-03-01T00:27:44Z")

</div>

Actually, you don’t know that.

Consider the following sequence:  
Person A fills out the form.  
Person B fills out the form.  
Person A submits the code.  
???

Keep in mind that the server does not keep a connection with the browser between requests. Each submit must be handled as an independent event.

---

<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 1, 2024, 12:32am UTC](https://forum.djangoproject.com/t/project-does-not-recognize-field/28530/13 "2024-03-01T00:32:57Z")

</div>

Do you have any idea how could I do this email verification? any experience with that? it’s not an odd algorithm. The user will only be allowed to access its account after the email verification

I’m thinking about a ‘code’ field in my model and add a field in my form, but the problem is that these seems to be separate things. I have to verify if the form is valid, create the user then confirm the code.

---

<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 1, 2024, 12:50am UTC](https://forum.djangoproject.com/t/project-does-not-recognize-field/28530/14 "2024-03-01T00:50:55Z")

</div>

The common process for doing something like this doesn’t include the user manually entering a code.

What I suggest you do is study the “Password reset” process (views and templates) to see a sample of how this type of mechanism works.

The short version is that when the user is created, a time-limited token is created that encodes the pk of that user. This token is sent as part of a url in a link in the email.

When the user clicks on that link, it takes them to the page that accepts the token as a parameter. That view then verifies the token as being valid, allowing the user to reset their password.

You can use this same process for this purpose.

---

<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 1, 2024, 12:56am UTC](https://forum.djangoproject.com/t/project-does-not-recognize-field/28530/15 "2024-03-01T00:56:29Z")

</div>

nice. I’ll use that.

I was thinking in that facebook/google way of creating a user (sending a little number code,  
using it in the text bar, registering the user) but the way you described seems good as well
