Answer the question
In order to leave comments, you need to log in
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: ['$']
#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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question