A
A
AinuRM2022-04-17 18:49:10
Django
AinuRM, 2022-04-17 18:49:10

How to automatically translate slug from a Russian word?

there is a model:

from django.db import models
from autoslug import AutoSlugField
from uuslug import uuslug

def instance_slug(self):
    return self.title

def slugify_value(value):
    return value.replace(' ', '-')

class Articles(models.Model):
    title = models.CharField(max_length=255, verbose_name="Заголовок")
    category = models.ForeignKey(Category, on_delete=models.PROTECT, verbose_name="Категория")
    preview = models.TextField(blank=True, verbose_name="Анонс")
    content = models.TextField(blank=False, verbose_name="Содержание")
    photo = models.ImageField(blank=True, upload_to="photos/%Y/%m/%d/", verbose_name="Картинка")
    time_create = models.DateTimeField(auto_now_add=True, verbose_name="Дата создания")
    is_published = models.BooleanField(default=False, verbose_name="Опубликовать")
    slug = AutoSlugField(max_length=50, db_index=True, unique=True, verbose_name='URL адрес',
                         populate_from=instance_slug, slugify=slugify_value)

    def save(self, *args, **kwargs):
        self.slug = uuslug(self.slug, instance=self)
        super(Articles, self).save(*args, **kwargs)

    def __str__(self):
        return self.title


625c36e357fa2656796185.png

everything works fine, the slug is automatically created. No complaints. But it is created in Russian. Why only with Russian names, what should I fix?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Shamanov, 2022-04-17
@SilenceOfWinter

do you even know what your code is doing? even to me, who is not familiar with python, it is obvious why you are not succeeding.

A
Alexander Nesterov, 2022-04-17
@AlexNest

But it is created in Russian.

What did you expect? So that it magically transforms into Latin? Alas, this is a rather specific problem - after all, not many countries use the Cyrillic alphabet.
Therefore, either implement transliteration manually, or use the library if you find it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question