Search code examples
djangoslug

Strange behaviour of slugify function


It works perfectly from the admin site. But the code below doesn't work properly(some characters are missing, like Turkish "ı") in some languages.

class Foo(models.Model):
    name = models.CharField(max_length=50, unique=True, db_index=True)
    slug = models.SlugField(max_length=100, unique=True, db_index=True)

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.name)
        super(Foo, self).save(*args, **kwargs)

For example, let's assume that the name is "ışçğö" and then slug becomes "scgo" when it should be "iscgo" instead.


Solution

  • This is SlugField behavior by definition. A slug is supposed to be part of a URL. Even though URLs might support non-latin characters, these are not supported inside slugs.

    Slug is a newspaper term. A slug is a short label for something, containing only letters, numbers, underscores or hyphens. They're generally used in URLs.


    The results you are getting aren't consistent with Django behavior:

    >>> from django.template.defaultfilters import slugify
    >>> v = u"ışçğö"
    >>> slugify(v)
    u'isg'
    

    Where exactly are you getting these results?