[Django]-Django slug field in Arabic and other foreign languages

5πŸ‘

βœ…

To enable url encoder, simply open settings.py and at the end of file, insert as new line:

ALLOW_UNICODE_SLUGS = True

πŸ‘€iraj jelodari

4πŸ‘

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.title)
            if not self.slug:
                self.slug = arabic_slugify(self.title)
        super(Note, self).save(*args, **kwargs)

    def __str__(self):
        return self.title


def arabic_slugify(str):
    str = str.replace(" ", "-")
    str = str.replace(",", "-")
    str = str.replace("(", "-")
    str = str.replace(")", "")
    str = str.replace("؟", "")
    return str
πŸ‘€user10157392

3πŸ‘

New to Django 1.9 is the setting allow_unicode:

slug = SlugField(allow_unicode=True)
πŸ‘€Flimm

1πŸ‘

It is better to use
allow_unicode=True attr for each SlugField that you want to make it allowing Unicode.
This is s simple example:

slug = models.SlugField(max_length=200, allow_unicode=True, unique=True)

For the new versions of Django the settings.py ALLOW_UNICODE_SLUGS = True attribute will not work.

0πŸ‘

check this https://github.com/mozilla/unicode-slugify , I have use it with Django 1.4.x/1.5.x, I was also looking for Arabic slugs.

πŸ‘€Mustafa

Leave a comment