B
B
Boldy2014-12-06 18:22:05
Django
Boldy, 2014-12-06 18:22:05

How to generate a separate page for a model in django?

For example, there is a Product model that has a SlugField
How do I make it so that when a new instance of Product is created, the page /products/new_product_slug/ is generated? What to write in url'ah and so on?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2014-12-06
@Boldy

First, override the save method on the model. So that when saving, a slug is created from something.
For this I use https://github.com/un33k/python-slugify

class Product(models.Model):
    title = models.CharField(max_lenght=220)
    slug = models.SlugField(unique=True, blank=True)
    
    def save(self, *args, **kwargs):
        self.slug = slugify(self.title)
        super(Product, self).save(*args, **kwargs)

And then define the urls:
And in the view already get products by the field of the slug. Naturally, it is better to make the field unique. I usually just add id at the end through '_'.

O
Oscar Django, 2014-12-06
@winordie

I guess something like:
where in <product_slug>
product -- the name of the model
slug -- the name of the SlugField of this model
Look -- this is , and in general read this lesson if you haven't already done so, everything is very well described there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question