# Email confirmation

**URL:** <https://forum.djangoproject.com/t/email-confirmation/18711>\
**Category:** Forms & APIs\
**Created:** [February 8, 2023, 10:39am UTC](https://forum.djangoproject.com/t/email-confirmation/18711 "2023-02-08T10:39:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hockney87](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hockney87/32/7225_2.png) [@Hockney87](https://forum.djangoproject.com/u/Hockney87)\
**Post date:** [February 8, 2023, 10:39am UTC](https://forum.djangoproject.com/t/email-confirmation/18711/1 "2023-02-08T10:39:59Z")

</div>

Hi ,  
I try to make a function that confirm email of a certain user by :

- Sending a link to user via email.
- This user must click on this link to authorize further acces to the website.

The first part works fine the function can send a token to the user .  
The second part didn’t work .

The code is divided in two functions 1 to create a user and send the mail the second to compare if a given token is valid for a given user.

 ![2023-02-08_11h43_48](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/8/8f5f1278c0239a949da65ef66da77a1eb2ebefc1.png)

 ![2023-02-08_11h44_01](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/6/6bc82e10904dcac5a3cdda0d86bc1e3552eef81d.png)

The second function return Activation link is invalid !

I tried to dig into the problem by examining the source code of the PasswordResetTokenGenerator class.

In the second function the code that i’m using employs a method of this class called check\_token . This check if a given reset token is valid for a given user.

 ![2023-02-08_11h45_00](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/9/904dea818e819ddcd9764469c8c53b9d009273d7.png)

Inside this method there is another methos used in a statement called : make\_token\_with\_timestamp.

This method compare 2 string and return a boolean if they are the same or not .

 ![2023-02-08_11h45_09](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/1/1b07c34d3dca7600ab8d53ccd7b7a8a22f4b63d5.png)

The init method of the PasswordGeneratorTokenGenerator class set self.algorithm = self.algorithm or “sha256”

The **\_make\_token\_with\_timestamps** generate a int\_to\_base\_36 - HMAC string  
but the HMAC string in generated with SHA1

I maybe completly wrong here but it is possible that the condition in my validate function didn’t become True due to to this difference of algorithms ?

---

<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 8, 2023, 1:07pm UTC](https://forum.djangoproject.com/t/email-confirmation/18711/2 "2023-02-08T13:07:03Z")

</div>

Side note: Please do not post images of code. They can’t be indexed or searched in the forum, and they can’t be copied and quoted to highlight issues in specific areas of code.  
Copy / paste your code into the body of your message surrounded by lines of three backtick - ` characters. This means that you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.

I’m a bit confused by what you have here. You’ve got multiple references to objects or variables that aren’t defined in what you have posted. My initial reaction to what I’m seeing here is that I would expect _this_ code to result in a 500 error on the page(s) referencing them.  
Please post the complete functions.

---

<div class="post-metadata">

**Author:** ![Hockney87](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hockney87/32/7225_2.png) [@Hockney87](https://forum.djangoproject.com/u/Hockney87)\
**Post date:** [February 8, 2023, 1:21pm UTC](https://forum.djangoproject.com/t/email-confirmation/18711/3 "2023-02-08T13:21:04Z")

</div>

> [@KenWhitesell](#):
>
> Side note: Please do not post images of code. They can’t be indexed or searched in the forum, and they can’t be copied and quoted to highlight issues in specific areas of code.  
> Copy / paste your code into the body of your message surrounded by lines of three backtick - ` characters. This means that you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.

Ok

> [@KenWhitesell](#):
>
> I’m a bit confused by what you have here. You’ve got multiple references to objects or variables that aren’t defined in what you have posted. My initial reaction to what I’m seeing here is that I would expect _this_ code to result in a 500 error on the page(s) referencing them.  
> Please post the complete functions.

All the functions are there . The first two come from my views.py and the last two come from the PassewordResetTokenGenerator class

---

<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 8, 2023, 1:30pm UTC](https://forum.djangoproject.com/t/email-confirmation/18711/4 "2023-02-08T13:30:35Z")

</div>

In your first function, you reference a variable named `account_activation_token`. I don’t see where that has been defined, nor do I find a system variable, function, or class by that name.

In your second function, you’re using that same variable name - again, without a definition.

We’re going to need to see **all** the code that’s involved in this process to be able to properly try and diagnose this.

---

<div class="post-metadata">

**Author:** ![Hockney87](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hockney87/32/7225_2.png) [@Hockney87](https://forum.djangoproject.com/u/Hockney87)\
**Post date:** [February 8, 2023, 1:41pm UTC](https://forum.djangoproject.com/t/email-confirmation/18711/5 "2023-02-08T13:41:03Z")

</div>

Yes Sorry i forgot this one

````auto
 from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six

class TokenGenerator(PasswordResetTokenGenerator):
    def _make_hash_value(self, user, timestamp):
        return (six.text_type(user.pk)+six.text_type(timestamp)+six.text_type(user.is_active))

account_activation_token=TokenGenerator() ```
````

---

<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 8, 2023, 1:51pm UTC](https://forum.djangoproject.com/t/email-confirmation/18711/6 "2023-02-08T13:51:31Z")

</div>

In your create\_user function, you’re not calling the `make_token` method on the `account_activation_token` object in your `render_to_string` call.

---

<div class="post-metadata">

**Author:** ![Hockney87](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hockney87/32/7225_2.png) [@Hockney87](https://forum.djangoproject.com/u/Hockney87)\
**Post date:** [February 8, 2023, 2:03pm UTC](https://forum.djangoproject.com/t/email-confirmation/18711/7 "2023-02-08T14:03:31Z")

</div>

No I tought it was auomaticly called or not when i instanciated the object in the render\_to\_string token variable context

---

<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 8, 2023, 2:16pm UTC](https://forum.djangoproject.com/t/email-confirmation/18711/8 "2023-02-08T14:16:05Z")

</div>

Nope. You need to call it. (Django calls it in `django.contrib.auth.forms.PasswordResetForm.save`)

---

<div class="post-metadata">

**Author:** ![Hockney87](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hockney87/32/7225_2.png) [@Hockney87](https://forum.djangoproject.com/u/Hockney87)\
**Post date:** [February 8, 2023, 2:42pm UTC](https://forum.djangoproject.com/t/email-confirmation/18711/9 "2023-02-08T14:42:58Z")

</div>

It worked ! Thank you for your help sir.

---

<div class="post-metadata">

**Author:** ![franciscomayele](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/franciscomayele/32/21214_2.png) [@franciscomayele](https://forum.djangoproject.com/u/franciscomayele)\
**Post date:** [May 11, 2024, 9:46am UTC](https://forum.djangoproject.com/t/email-confirmation/18711/10 "2024-05-11T09:46:27Z")

</div>

Deat all, can you guys be more clear in how to procced with that

Hi ,

Your account has successfully created. Please click below link to activate your account

[http://127.0.0.1:8000/account/activate/MTI/c6u8b2-28d19f943b7cc7c4fa30f842e2c0d020](http://127.0.0.1:8000/account/activate/MTI/c6u8b2-28d19f943b7cc7c4fa30f842e2c0d020))/

* * *

[11/May/2024 01:04:15] “POST /account/register/ HTTP/1.1” 200 9648  
Internal Server Error: /account/activate/MTE/c6u6wk-d7342c0c23d80eda7a7ef53a856eb85d)/  
Traceback (most recent call last):  
I’m still facing issue
