Boris Gaganelov Boris Gaganelov - 7 months ago 60
Python Question

Sharing URL generation from uuid4?

I'm having a bit of a problem figuring out how to generate user friendly links to products for sharing.

I'm currently using


Which is working quite fine - but it's a bit user unfriendly - it's kind of long and ugly.

And I do not wish to use and id as it would allow users to "guess" products. Not that that is too much of an issue - I would like to avoid it.

Do you have any hints on how to generate unique, user friendly, short sharing urls based on the unique item id or uuid?


As Seluck suggested I decided to go with base64 encoding and decoding:

In the model my "link" property is now built from the standard url + base64.urlsafe_b64encode(str(media_id))

The url pattern I use to match the base64 pattern:

base64_pattern = r'(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$'

And finally in the view we decode the id to load the proper data: media_id = base64.urlsafe_b64decode(str(media_id))

media = Media.objects.get(pk=media_id)