Django模板中产品列表的过滤类别

发布于 2025-02-04 06:12:35 字数 571 浏览 0 评论 0 原文

因此,我在此Django电子商务网站上工作,我应该根据三个单独的类别在页面上过滤这些产品。由于只有三个固定类别,因此我决定在我的模型类中为产品创建一个字典,并认为将有一种方法可以在我的模板中使用{%for%}循环过滤我的模板中的产品。

它无法正常工作,而且我会遇到错误,这可能是因为我对Django并不那么熟悉,也不知道如何解决它。非常感谢您的帮助! (我已经为上下文附加了一些屏幕截图)

models.products screenshot

为循环的工作实施,直接显示所有产品

Views.catalogue

So I'm working on this Django e-commerce website where I'm supposed to filter the products on my page based on three separate categories. Since there were only three fixed categories, I decided to create a dictionary in my model class for Products and thought there would be a way to filter products later on in my templates accordingly using a {%For%} loop.

It's not working as I expected tho and I'm getting errors, probably because I'm not that familiar with Django and don't know how to get my way around it. Would greatly appreciate the help! (I've attached some screenshots for context)

MODELS.products screenshot

working implementation of for loop that directly shows all products

Views.catalogue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

燃情 2025-02-11 06:12:35

我的建议是,您在的内部添加锚定标签,它将具有类别名称,然后您可以将其作为关键字到定义的 url 类别。

例如:

html 中...

{% for product in products %}
     ...
     # Adding this anchor tag to your existing code within the loop that will display each product's category.
     # Also you can now send this category name to a specific view to filter the products
     
     <a href="{% url 'category' product.category %}">{{ product.category|title }}</a>
     # Notice that I have passed product.category as a keyword in {% url 'category' product.category %} 
     ...
{% endfor %}

现在在您的 urls.py 文件中,只需创建一个url以处理以关键字为关键字的类别。

from .views import category_view  # Importing the new view

urlpatterns = [
     ...    
     path('category/<str:category>/', category_view, name='category'),
     ...
]

views.py 文件。

def category_view(request, category): # Accepting the category passed here
     # You can have any other operations in here if necessary

     products = Product.objects.filter(category__iexact=category)

     context = {
          ...
          'products': products,
          ...
     }
     return render(request, 'products.html', context)

这就是解决问题的基本概念。

My recommendation is that you add an anchor tag inside of the for loop that will have the category name which you can then pass as a keyword to a defined url for categories.

For example:

In the html...

{% for product in products %}
     ...
     # Adding this anchor tag to your existing code within the loop that will display each product's category.
     # Also you can now send this category name to a specific view to filter the products
     
     <a href="{% url 'category' product.category %}">{{ product.category|title }}</a>
     # Notice that I have passed product.category as a keyword in {% url 'category' product.category %} 
     ...
{% endfor %}

Now within your urls.py file, just create a url to handle categories passed as keywords.

from .views import category_view  # Importing the new view

urlpatterns = [
     ...    
     path('category/<str:category>/', category_view, name='category'),
     ...
]

From the views.py file.

def category_view(request, category): # Accepting the category passed here
     # You can have any other operations in here if necessary

     products = Product.objects.filter(category__iexact=category)

     context = {
          ...
          'products': products,
          ...
     }
     return render(request, 'products.html', context)

That's the basic concept of what would solve your problem.

ヤ经典坏疍 2025-02-11 06:12:35

我正在为解决方案使用基于班级的视图。基本上,您想按类别正确显示产品列表。因此解决方案如下。

在views.py上,视图类是这样:

from django.views.generic import ListView
from .models import Product

class ProductListView(ListView):
    model = Product
    queryset = model.objects.all()
    context_object_name = 'products'
    template_name = 'your-template-path'

    def get_queryset(self):
        queryset = super(ProductListView, self).get_queryset()
        category = self.request.GET.get('category', None)
        if category:
            return queryset.filter(category=category)
        return queryset

现在使用类别关键字在URL参数上发送类别值。我没有使用任何身份验证系统。请自己实施

I am using Class Based Views for the solutions. Basically you want to show the list of the products by category right. So the solution is as follows.

on views.py the view class is like this:

from django.views.generic import ListView
from .models import Product

class ProductListView(ListView):
    model = Product
    queryset = model.objects.all()
    context_object_name = 'products'
    template_name = 'your-template-path'

    def get_queryset(self):
        queryset = super(ProductListView, self).get_queryset()
        category = self.request.GET.get('category', None)
        if category:
            return queryset.filter(category=category)
        return queryset

Now send the category values on your URL parameter using category keyword. I am not using any authentication system. Please implement it on your own

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文