How to encode/decode string using django.utils.http

Hi there,

I am trying to pass a user ID (primary key) to url as base 64 encoded. I am using django.utils.http.urlsafe_base64_encode to encode the user ID as the following:

from django.utils.http import urlsafe_base64_encode

user_id_base_64 = urlsafe_base64_encode(str(user.pk).encode('utf-8'))

This works fine and I get the desired 64 base byte of user id. The url pattern to hold this user id 64 base is:

path("password-reset/<uidb64>/, views.dashboard())

But I need to decode the same in the view which is accessed by the above url.

I try to do that with the following:

from django.utils.http import urlsafe_base64_decode

.
.
.
user_id = urlsafe_base64_decode(uidb64)

This does not give me the correct result, i.e. a numeric string or an integer.

Can someone help me how do I decode it properly to get the correct value?

Most obvious to me is the mistake here. Unless you’re doing something really unusual here, the definition would be:
path("password-reset/<uidb64>/", views.dashboard)

Beyond that, it would be helpful if you showed an example of one of the keys, what that translates to for the url, and what you’re seeing in the view.

Actually it was just a typo here in this post. In reality I have the same definition of path as you told in my code.

Well, the I have the following output when I print user.key:

>>> from .models import User
>>> user = User.objects.get(pk=1)
>>> print(user.pk)
1

and when I get the following output of encoding:

>>> from django.utils.http import urlsafe_base64_encode
>>> user_id_base_64 = urlsafe_base64_encode(str(user.pk).encode('utf-8'))
>>> print(user_id_base_64)
MQ

Now when I try to decode, I get the following output:

>>> from django.utils.http import urlsafe_base64_decode
>>> user_key = urlsafe_base64_decode(user_id_base_64)
>>> print(user_key)
b'1'

So, you can see instead of getting back 1 either in form of str or int, I am getting b'1'.

It’s decoding as a bytes object; you can then decode it as a string.

Doing the following:

user_key = urlsafe_base64_decode(user_id_base_64.decode('str'))

gives me the following error:

Traceback (most recent call last):
  File "<string>", line 6, in <module>
AttributeError: 'bytes' object has no attribute 'encode'

It’s the output of urlsafe_base64_decode that is returning the bytes object, what you are printing as user_key - that is what you want to decode as a string.

Upon doing the following

user_key = urlsafe_base64_decode(user_key_base_64).decode('str')

I get the following error:

Traceback (most recent call last):
  File "<string>", line 6, in <module>
LookupError: unknown encoding: str

@KenWhitesell any solution to this problem?

What do you think that error message is telling you?

Its saying that it does not recognize str encoding. So what should I do to make it recognize or what’s the alternative method?

Did you read the documentation I referenced? It shows what all the valid encodings are.

2 Likes

OK, I got it, I changed 'str' to 'ascii' and I got the original string back, now just needed to convert it to integer using int() method. Thanks a lot.

1 Like