Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
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.
But it is created in Russian.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question