2

I tried to generate uid for a user confirmation email.

'uid':urlsafe_base64_encode(force_bytes(user.pk)),

so, it's works nice, it returns something like "Tm9uZQ"

Then, when I tried to decode it, using force_text(urlsafe_base64_decode(uidb64)) it return None.

The next string

urlsafe_base64_decode(uidb64)

also, return b'None'

I tried to google it, and see different implementations, but copy-paste code not works.

I write something like

    b64_string = uidb64
    b64_string += "=" * ((4 - len(b64_string) % 4) % 4)
    print(b64_string)
    print(force_text(base64.urlsafe_b64decode(b64_string)))

and the result still None:

Tm9uZQ==
None

I don't understand how the default decode doesn't work.

1 Answer 1

4

"Tm9uZQ==" is the base64 encoding of the string "None",

>>> from base64 import b64encode, b64decode
>>> s = b'None'
>>> 
>>> b64encode(s)
b'Tm9uZQ=='
>>> b64decode(b64encode(s))
b'None'
>>> 

It could be possible that some of your data is missing. E.g. user.pk is not set. I think that force_bytes is turning a None user.pk into the bytestring b'None', from the Django source,

def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
    """
    Similar to smart_bytes, except that lazy instances are resolved to
    strings, rather than kept as lazy objects.

    If strings_only is True, don't convert (some) non-string-like objects.
    """
    # Handle the common case first for performance reasons.
    if isinstance(s, bytes):
        if encoding == 'utf-8':
            return s
        else:
            return s.decode('utf-8', errors).encode(encoding, errors)
    if strings_only and is_protected_type(s):
        return s
    if isinstance(s, memoryview):
        return bytes(s)
    return str(s).encode(encoding, errors)

You might be able to prevent None being turned into b'None' by setting strings_only=True when calling force_bytes.

Sign up to request clarification or add additional context in comments.

1 Comment

Your right, I tried to get the user from a request, but I use REST API for this functionality. Because user token is empty, I get AnonimousUser without Id (it's just None).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.