0

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

I'm currently using /product/{uuid4_of_said_product} 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?

3
  • Are slugs appropriate for your use case? Commented Jun 1, 2016 at 12:40
  • Try doing a search for base62 encoder/decoder libraries. Still guessable but much shorter. Commented Jun 1, 2016 at 12:41
  • Greetings raphv - yes slugs are not a possibility as the items in question do not have a title/name or anything that can be slugified. I apologize for forgetting to mention this from the get go. I will look into those Selcuk - thanks. I did not think of investigation a simple base 64 encode. Silly me. Commented Jun 1, 2016 at 14:09

2 Answers 2

1

Have you tried these https://github.com/corpix/shortid and one for django here https://github.com/nebstrebor/django-shortuuidfield

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

Comments

1

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)

Comments

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.