因此,我在此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
发布评论
评论(2)
我的建议是,您在的内部添加
锚定标签
,它将具有类别名称
,然后您可以将其作为关键字到定义的
url
类别。例如:
在
html
中...现在在您的
urls.py
文件中,只需创建一个url以处理以关键字为关键字的类别。从
views.py
文件。这就是解决问题的基本概念。
My recommendation is that you add an
anchor tag
inside of thefor loop
that will have thecategory name
which you can then pass as akeyword
to a definedurl
for categories.For example:
In the
html
...Now within your
urls.py
file, just create a url to handle categories passed as keywords.From the
views.py
file.That's the basic concept of what would solve your problem.
我正在为解决方案使用基于班级的视图。基本上,您想按类别正确显示产品列表。因此解决方案如下。
在views.py上,视图类是这样:
现在使用类别关键字在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:
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