Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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 :)

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top