C
C
ckatala2020-06-15 08:11:25
Django
ckatala, 2020-06-15 08:11:25

How to change the slug to the desired view?

You need to save the slug in this form
site.com/123-blablabla
. I do this in the Model

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)

url
path('articles/<slug:slug>/', DetailArticleView.as_view(), name='article_detail'),

I receive
site.com/none-blablabla

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem, 2020-06-15
@ckatala

none turns out because the slug is set before the record is saved and self.id does not work as expected, there is no record in the database yet and there is no id itself. Take advantage of signals

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question