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)
To enable url encoder, simply open settings.py and at the end of file, insert as new line:
ALLOW_UNICODE_SLUGS = True
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With