V
V
Vova1357982021-05-18 07:58:56
Django
Vova135798, 2021-05-18 07:58:56

How to go to a page by Slug?

How to navigate to a page created with detailview in html?

view.py

class ProductDet(DetailView):
    model = Product
    context_object_name = 'product'
    template_name = 'detail_prod.html'


urls.py


urlpatterns = [
    path('', Homepage.as_view(), name='homepage'),
    path('<slug:slug>/', ProductDet.as_view(), name='product'),
]


models.py


class Product(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField(blank=True, null=True)
    price = models.DecimalField(max_digits=9, decimal_places=0)
    img = models.ImageField(upload_to='photos/%y/%m/', blank=True)
    category = models.ForeignKey('Category', on_delete=models.CASCADE)
    slug = models.SlugField(unique=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Melnikov, 2021-05-23
@Mi11er

<a href="{% url "product" slug=obj.slug %}"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question