python - django 1.9 slug field is not working for foreign language -
i building post app, automatically creates slug post title. if there foreign language in title, slug not getting generated.
i have gone through of answers here, it's not helping much. missing in below ?
class post(models.model): title = models.charfield(max_length=120) slug = models.slugfield(unique=true, allow_unicode=true) content = models.textfield() def create_slug(instance, new_slug=none): slug = slugify(instance.title) if new_slug not none: slug = new_slug qs = post.objects.filter(slug=slug).order_by("-id") exists = qs.exists() if exists: new_slug = "%s-%s" %(slug, qs.first().id) return create_slug(instance, new_slug=new_slug) return slug def pre_save_post_receiver(sender, instance, *args, **kwargs): if not instance.slug: instance.slug = create_slug(instance)
added below in settings.py
:
allow_unicode_slugs = true
you need tell slugify
should allow unicode, too. see docs.
def create_slug(instance, new_slug=none): slug = slugify(instance.title, allow_unicode=true)
also, careful: default max_length
slugfield
is 50 characters. converting long title may result in slug long slugfield
, raise exception.
Comments
Post a Comment