D
D
Dzhemchik2020-02-05 04:50:06
Django
Dzhemchik, 2020-02-05 04:50:06

How to return url from model to template?

Hello! Stuck, need your help.
It is not possible to return references from the model to the template. Using normal functions, everything worked. Now the task is to pass through generic.ListView - it does not work in any way. The error is the following:

NoReverseMatch at /
Reverse for 'product_list' with arguments '('saws',)' not found. 1 pattern(s) tried: ['$']

It turns out that the element of a tuple is returned?

#models
class Category(models.Model):
    name = models.CharField(verbose_name='Категория', max_length=100, db_index=True)
    slug = models.SlugField(max_length=100, db_index=True,
                            unique=True)
...
    def get_absolute_url(self):
        return reverse('core:product_list',
                       args=[self.slug])


#urls
app_name = 'core'

urlpatterns = [
    path('', views.ProductView.as_view(), name='product_list'),]

views.py
class ProductView(generic.ListView):
    queryset = Product.objects.filter(available=True)
    categories = Category.objects.all()


    def get_context_data(self, **kwargs):
        context = super(ProductView, self).get_context_data(**kwargs)
        ...
        context['categories'] = self.categories
        return context

#html. inside block
{% for c in categories %}
    <a href="{{ c.get_absolute_url }}">{{ c.name }}</a>
{% endfor %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WStanley, 2020-02-05
@dzhem911

You pass a slug to product_list but do it without respect and don't accept it doc link
doc
link 2 (example)

path('  тут принять slug  ', views.ProductView.as_view(), name='product_list'),]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question