site.com/123-blablabla
class Article(models.Model):
title = models.CharField(max_length=120, null=False)
slug = models.SlugField(null=False, blank=True, unique=True)
def save(self, *args, **kwargs):
self.slug = slugify(f"{str(self.id)}-{self.title}", lowercase=True)
super().save(*args, **kwargs)
path('articles/<slug:slug>/', DetailArticleView.as_view(), name='article_detail'),
site.com/none-blablabla
slugify
выкидывает цифры. попробуйте str(self.id) + "-" + slugify(self.title, lowercase=True)
signals.post_save.connect(create_slug, sender=Articles)
def create_slug(sender, instance, signal, *args, **kwargs):
if instance.id and hasattr(instance, 'slug_field_name') and hasattr(instance, 'slug_from'):
slug_name = instance.slug_field_name
slug_from = instance.slug_from
if not getattr(instance, slug_name, None):
slug = '%s-' % instance.id + slugify(getattr(instance, slug_from))
setattr(instance, slug_name, slug)
instance.save()