Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django slug field in Arabic and other foreign languages

Tags:

django

arabic

am trying to build an app with a model which contains a unique of the title, some users will write the title in Arabic and other languages and when on save, I try to slugify the field. It would return blank in case of Arabic. Any way to show arabic wordings but maintain slug principles of replacing special characters and spaces with dash?

class Article(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField()

   def save(self, *args, **kwargs):
     if self.slug is in (None, '', u''):
       self.slug = slugify(self.title)
     super(Article, self).save(*args, **kwargs)
like image 229
Mo J. Mughrabi Avatar asked Oct 15 '25 13:10

Mo J. Mughrabi


2 Answers

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

ALLOW_UNICODE_SLUGS = True

like image 127
iraj jelodari Avatar answered Oct 17 '25 06:10

iraj jelodari


    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
like image 32
user10157392 Avatar answered Oct 17 '25 04:10

user10157392



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!