On python 3.6 with django 2.0, decode on a byte literal does not work as expected.
Yes I get the right result when I print it, but the b'value'
is still there even if you print it right.
This is what I'm encoding
uid': urlsafe_base64_encode(force_bytes(user.pk)),
This is what I'm decoding:
uid = force_text(urlsafe_base64_decode(uidb64))
This is what django 2.0 says :
urlsafe_base64_encode(s)[source]
Encodes a bytestring in base64 for use in URLs, stripping any trailing equal signs.
urlsafe_base64_decode(s)[source]
Decodes a base64 encoded string, adding back any trailing equal signs that might have been stripped.
This is my account_activation_email_test.html file
{% autoescape off %}
Hi {{ user.username }},
Please click on the link below to confirm your registration:
http://{{ domain }}{% url 'accounts:activate' uidb64=uid token=token %}
{% endautoescape %}
This is my console response:
Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0
Content-Transfer-Encoding: 7bit Subject: Activate Your MySite Account
From: webmaster@localhost To: testuser@yahoo.com Date: Fri, 20 Apr
2018 06:26:46 -0000 Message-ID:
<152420560682.16725.4597194169307598579@Dash-U>
Hi testuser,
Please click on the link below to confirm your registration:
http://127.0.0.1:8000/activate/b'MjU'/4vi-fasdtRf2db2989413ba/
as you can see uid = b'MjU'
expected uid = MjU
test in console:
$ python
Python 3.6.4 (default, Apr 7 2018, 00:45:33)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
>>> from django.utils.encoding import force_bytes, force_text
>>> var1=urlsafe_base64_encode(force_bytes(3))
>>> print(var1)
b'Mw'
>>> print(var1.decode())
Mw
>>>
After investigating it seems like its related to python 3.
My workaround was quite simple:
'uid': user.pk,
I receive it as uidb64 on my activate function:
user = User.objects.get(pk=uidb64)
and voila:
Content-Transfer-Encoding: 7bit
Subject: Activate Your MySite Account
From: webmaster@localhost
To: testuser@yahoo.com
Date: Fri, 20 Apr 2018 20:44:46 -0000
Message-ID: <152425708646.11228.13738465662759110946@Dash-U>
Hi testuser,
Please click on the link below to confirm your registration:
http://127.0.0.1:8000/activate/45/4vi-3895fbb6b74016ad1882/
now it works fine.