Home > Blockchain >  products not showing in product view page django
products not showing in product view page django

Time:11-08

i created a function to view products in products view page i tried so many times but cant find any one help me please

here is my code

views.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)):
            product = Products.objects.filter(slug=prod_slug, status=0)
            contex = {
                'product':product,
            } 
        else:
            messages.warning(request,"product  not found") 
            return redirect("collection")
    else:
        messages.error(request,"something went wrong")
        return redirect("collection")
    return render(request,"product_view.html",contex)
   
urls.py



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





product view.html



{% extends 'auth.html' %} {% load static %}

{% block content %}

{{ products.name }}

{% endblock %}








i just want to see product detils in product view html page any one help me because i am new to django

CodePudding user response:

You are using {{product.name}} which is wrong. products (actually product based on your view) is a queryset and it doesnt have a name. Instead first change your queryset name to a more distinquishable products, then use this:

{% for product in products %}
{{ product.name }} 

CodePudding user response:

In views:

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)):
            product = Products.objects.filter(slug=prod_slug, status=0)
          
        else:
            messages.warning(request,"product  not found") 
            return redirect("collection")
    else:
        messages.error(request,"something went wrong")
        return redirect("collection")
    return render(request,"product_view.html",{'product':product}) #I have removed context from if statement and added directly here.

And in your templates:

{{product.name}} #Use this only, when you want to display single field only.

OR

{% for prod in product %} #Use this only, when you want to display all fields on template
   {{prod.name}}
   #Add remaining fields
{% endfor %}

And see if it solves

  • Related