【问题标题】:django eccomerce prodcut name not showing in filedjango电子商务产品名称未显示在文件中
【发布时间】:2022-11-07 22:19:41
【问题描述】:

我现在正在产品详细信息页面中创建一个新的 django eccomorce 网站这是我的代码

问题是我在 first() 的 html 页面问题中看不到正确的产品名称 当我第一次使用时,只显示产品名称,但所有产品都具有相同的名称我在我的第 8 页中有 8 个产品,产品名称与第一个相同,就像覆盖一样,我也不能使用 first() 进行循环

我会添加一些图片

网址.py

 path('collection/<str:cate_slug>/<str:prod_slug>',views.product_view,name="productview"),

视图.py

def product_view(request,cate_slug,prod_slug):
    if (Category.objects.filter(slug=cate_slug, status=0)):
        if (Products.objects.filter(slug=prod_slug, status=0)):
            products = Products.objects.filter(slug=prod_slug, status=0).first()
            context = {'products':products}
        else:
            messages.error(request,"no such product found")
            return redirect("collection")
    else:
        messages.error(request,"no such category found")
        return redirect("collection")
    
    return render(request,"product_view.html",context)

模型.py

class Products(models.Model):
    category = models.ForeignKey(Category,on_delete=models.CASCADE)
    slug = models.CharField(max_length=150, null=False, blank=False)
    product_name = models.CharField(max_length=150, null=False, blank=False)
    product_image = models.ImageField( upload_to=get_image,null=True,blank=True)
    description = models.TextField(max_length=500,null=False,blank=False)
    original_price = models.IntegerField(null=False,blank=False)
    selling_price = models.IntegerField(null=False,blank=False)
    status = models.BooleanField(default=False,help_text="0=default , 1=Hidden")
    trending = models.BooleanField(default=False,help_text="0=default , 1=Trending")
    meta_title = models.CharField(max_length=150,null=False,blank=False)
    meta_keyword = models.CharField(max_length=150,null=False,blank=False)
    meta_description = models.CharField(max_length=400,null=False,blank=False)
    created_at = models.DateTimeField(auto_now_add=True)
    
    def __str__(self):
        return self.product_name
    

productview.html

{% block content %}

 <h1>{{ products.product_name }} </h1>
 



{% endblock %}

我只想为我早上在这里停留的每个类别提供正确的产品名称,感谢大家的帮助,感谢大家到目前为止的帮助

【问题讨论】:

    标签: django django-models django-views django-templates


    【解决方案1】:

    我认为您需要遍历您的产品:

    {% for product in products %}
      <h1>{{ product.product_name }}</h1>
    {% endfor %}
    

    注意,通常型号名称是单数,所以应该是class Products(models.Model):

    【讨论】:

      猜你喜欢
      • 2019-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      相关资源
      最近更新 更多