user256604
user256604

Reputation: 303

How to generate shareable links in django? (similar to pastebin links)

One feature I would like to add to my django app is the ability for users to create some content (without signing up / creating an account), and then generating a content-specific link that the users can share with others. Clicking on the link would take the user back to the content they created.

Basically, I'd like the behavior to be similar to sites like pastebin - where users get a pastebin link they can share with other people (example: http://pastebin.com/XjEJvSJp)

I'm not sure what the best way is to generate these types of links - does anyone have any ideas?

Thanks!

Upvotes: 0

Views: 2363

Answers (4)

Juliusz Gonera
Juliusz Gonera

Reputation: 4958

If you don't mind that your URLs will get a bit longer you can have a look at the uuid module. This should guarantee unique IDs.

Upvotes: 1

Assaf Lavie
Assaf Lavie

Reputation: 76053

Basically you just need a view that stores data and a view that shows it.

e.g. Store with:

server.com/objects/save

And then, after storing the new model, it could be reached with

server.com/objects/[id]

Where [id] is the id of the model you created when you saved.

This doesn't require users to sign in - it could work for anonymous users as well.

Upvotes: 0

AndiDog
AndiDog

Reputation: 70198

You can create these links in any way you want, as long as each link is unique. For example, take the MD5 of the content and use the first 8 characters of the hex digest.

A simple model for that could be:

class Permalink(models.Model):
    key = models.CharField(primary_key = True, max_length = 8)
    refersTo = models.ForeignKey(MyContentModel, unique = True)

You could also make refersTo a property that automatically assigns a unique key (as described above).

And you need a matching URL:

url("^permalink/(?P<key>[a-f0-9]{8})$",
    "view.that.redirects.to.permalink.refersTo"),

You get the idea...

Upvotes: 1

Amber
Amber

Reputation: 526973

Usually all that is made up of is a (possibly random, possibly sequential) token, plus the content, stored in a DB and then served up on demand.

Upvotes: 1

Related Questions