views:

525

answers:

4

I'm currently working on a toy project in Django.

Part of my app allows users to leave reviews. I'd like to take the title of the review and slugify it to create a url.

So, if a user writes a review called "The best thing ever!", the url would be something like: www.example.com/reviews/the-best-thing-ever.

That's all well and good, but what is the best way to handle case where two users pick the same title? I don't want to make the title required to be unique.

I've thought about adding the review id in the url somewhere, but I'd like to avoid that extra info for any urls that don't collide.

Any ideas?

+1  A: 
from django.template.defaultfilters import slugify

def slugify_unique(value, model, slugfield="slug"):
        suffix = 0
        potential = base = slugify(value)
        while True:
            if suffix:
                potential = "-".join([base, str(suffix)])
            if not model.objects.filter(**{slugfield: potential}).count():
                return potential
            suffix += 1      
"""
above function is not my code, but i don't remember exactly where it comes from
you can find many snippets with such solutions searching for 'unique slug' and so
"""


class ReviewForm(forms.ModelForm):

    def save(self, user, commit=True):    
        self.instance.slug = slugify_unique(self.cleaned_data['title'], self.Meta.model)                       
        review = super(ReviewForm, self).save(commit)
        review.save()
        return review

    class Meta:
        model = Review

of course change the appropriate names and form definition, but you get the idea :)

zalew
+2  A: 

I would recommend something like AutoSlugField. It has a few options available with respect to configuring uniqueness (unique and unique_with), and has the added benefit of being able to automatically generate slugs based on another field on your model, if you so choose.

Ryan Duffield
+3  A: 

One thing I never liked about the unique slug fields/methods is that if you have a lot of clashes for a single title, you'll end up running several queries to try and determine an available slug. I know you mentioned you don't want to show the id for non-clashing slugs, but, as far as performance, I think it's the better route to take. To make the URL a little nicer looking, I prefer also to embed the id before the slug, so that a URL takes the form of www.example.com/reviews/1/the-best-thing-ever.

Adam
This is the solution I was considering, although in this case, slug turns out to be meaningless eye candy. It's fast though and frees you from this problem entirely. In a "real" app i'd probably take this route, but I'm still interested in how one would handle this and still allow non-colliding urls to be totally free of any ids.
TM
I should also note that this seems to be the same approach that SO uses, if you glance up at the url bar :)
TM
Well, it looks like I'm in good company then. As far as eliminating all ids, the two other answers are definitely the way to go. When I originally started slugging things, I used a unique slug method I found somewhere (it's nearly identical to the one Zalew posted).
Adam
I wouldn't call it meaningless eye candy, it still serves its purpose for SEO.
Shinhan
A: 

I would (in the form validation) just check to see if the slug is used, and then add something to it, either a number "my-cool-idea_2" or the actual id

Jared Forsyth