Search code examples
djangourlidentifier

Django - URL design and best practices for identify one object


Im actually working in a django project and I'm not sure about the best format of the URL to access into one particular object page.

I was thinking about these alternatives:

1) Using the autoincremental ID => .com/object/15

This is the simplest and well known way of do that. "id_object" is the autoincremental ID generated by the database engine while saving the object. The problem I find in this way is that the URLs are simple iterable. So we can make an simple script and visit all the pages by incrementing the ID in the URL. Maybe a security problem.

2) Using a <hash_id> => .com/object/c30204225d8311e185c3002219f52617

The "hash_id" should be some alphanumeric string value, generated for example with uuid functions. Its a good idea because it is not iterable. But generate "random" uniques IDs may cause some problems.

3) Using a Slug => .com/object/some-slug-generated-with-the-object

Django comes with a "slug" field for models, and it can be used to identify an object in the URL. The problem I find in this case is that the slug may change in the time, generating broken URLs. If some search engine like Google had indexed this broken URL, users may be guided to "not found" pages and our page rank can decrease. Freezing the Slug can be a solution. I mean, save the slug only on "Add" action, and not in the "Update" one. But the slug can now represent something old or incorrect.

All the options have advantages and disadvantages. May be using some combination of them can some the problems. What do you think about that?


Solution

  • I think the best option is this:

    .com/object/AUTOINCREMENT_ID/SLUG_FIELD
    

    Why?

    First reason: the AUTOINCREMENT_ID is simple for the users to identify an object. For example, in an ecommerce site, If the user want to visit several times the page (becouse he's not sure of buying the product) he will recognize the URL.

    Second reason: The slug field will prevent the problem of someone iterating over the webpage and will make the URL more clear to people.

    This .com/object/10/ford-munstang-2010 is clearer than .com/object/c30204225d8311e185c3002219f52617