Django: List Products of each Categories in a page

本小妞迷上赌 提交于 2019-12-06 14:12:38

问题


I have a few categories and I would like to list the products per category in the format below (categories is an FK to products):

Category 1

bunch of products

....

Category N

bunch of products

I have tried many ways but so far I only get the categories but not the products to show in my HTML.

models.py

class Category(models.Model):
    title = models.CharField(max_length=225)
    slug = models.SlugField(unique=True, blank=True, null=True)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('category_detail', kwargs={'slug': self.slug})

    @property
    def get_products(self):
         return Products.objects.filter(category=self.title)

class Products(models.Model):
    title = models.CharField(max_length=225)
    image = models.ImageField(upload_to=upload_image_path, null=True, blank=True)
 blank=True, on_delete=models.CASCADE)
    categories = models.ForeignKey(Category, related_name='Category', blank=True, on_delete=models.CASCADE) #this
    gender = models.CharField(max_length=20, choices=GENDER_CHOICES, default="male")

    objects = ProductManager()

    def get_absolute_url(self):
        return reverse('product_detail', args=(self.id,))

    def __str__(self):
        return self.title

views.py

def categories_m(request):
    query_Set = models.Category.objects.all()
    page = request.GET.get('page', 1)
    paginator = Paginator(query_Set, 20)
    try:
        cat = paginator.page(page)
    except PageNotAnInteger:
        cat = paginator.page(1)
    except EmptyPage:
        cat = paginator.page(paginator.num_pages)
    return render(request, 'products/categories.html', {'categories': cat})

html

{% extends 'base.html' %}

{% block content %}
{% for category in categories %}
    <h1>{{ category }}</h1>
    {% for product in categories.get_products %}
        <p>{{ product }}</p>
    {% endfor %}
{% endfor %}
{% endblock %}

回答1:


It should be category instead of categories in your template. And in your model filter products by category title.

@property
def get_products(self):
     return Products.objects.filter(categories__title=self.title)


{% extends 'base.html' %}

    {% block content %}
        {% for category in categories %}
        <h1> {{category}} </h1>

        {% for product in category.get_products %}
            <p> {{product}} </p>
        {% endfor %}

        {% endfor %}
    {% endblock %}



回答2:


You could remove the related_name attribute from your field

class Products(models.Model):
    categories = models.ForeignKey(Category, blank=True, on_delete=models.CASCADE)

And then in your template you can use category.products_set.all

{% extends 'base.html' %}

    {% block content %}
    {% for category in categories %}
        <h1>{{ category }}</h1>
        {% for product in category.products_set.all %}
            <p>{{ product }}</p>
        {% endfor %}
    {% endfor %}
    {% endblock %}


来源:https://stackoverflow.com/questions/49948016/django-list-products-of-each-categories-in-a-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!