Home > Software design >  Referencing a field in Django template gives output None
Referencing a field in Django template gives output None

Time:12-29

I've got a problem. I don't know how to reference product category on my for loop in Django Template. I have added a random category to a Product (in database), but no matter which category i choose, the output on the site from {{product.product_category.name}}is "None" :

def all_products(request):
    product = Product.objects.all().order_by('-created_at').a
    paginator = Paginator(product, 25)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    context = {
        'page_obj': page_obj,
    }
    return render(request, 'ecommerce/all_products.html', context)

then my for loop on Django template looks like this

            {% for product in page_obj %}
                    <div >
                        <a  href="{% url 'ecommerce:product_detail_view' product.slug %}">
                            <p >{{ product.product_producer }}</p>
                            <p >{{ product.name }}</p>
                            <p >{{ product.product_category.name }}</p>
                        </a>
                    </div>
                {% endfor %}

Model product looks like this

class Product(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200, unique=True, null=False, editable=False)
    created_at = models.DateTimeField(editable=False, default=timezone.now)
    updated_at = models.DateTimeField(default=timezone.now)
    product_category = models.ManyToManyField(EcommerceProductCategory)
    description = RichTextField(max_length=2000, null=True, blank=True)
    product_producer = models.ForeignKey('ProductProducer', on_delete=models.CASCADE)
    creator = models.ForeignKey('users.CustomUser', on_delete=models.CASCADE, null=True, blank=True, related_name='product_creator')
    points_from_reviews = models.DecimalField(max_digits=6, decimal_places=2, default=0, help_text='Średnia ocena produktu')
    unique_id = models.CharField(max_length=256, unique=True)
    type_of_unique_id = models.CharField(max_length=64)
    product_img = models.FileField(upload_to='ecommerce_product_img/', null=True, blank=True)

And the categories are connected with EcommerceProductCategory

class EcommerceProductCategory(MPTTModel):
    name = models.CharField(max_length=64, null=True, unique=False)
    slug = models.SlugField(max_length=40, unique=False, null=False, editable=False)
    created_at = models.DateTimeField(editable=False, default=timezone.now)
    updated_at = models.DateTimeField(default=timezone.now)
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')

Do you have any idea whats wrong?

CodePudding user response:

Your product_category = models.ManyToManyField(EcommerceProductCategory) is a M2M field it contain queryset you've to iterate over it to get all names belong to current product like this

{% for product in page_obj %}
   <div >
     <a  href="{% url 'ecommerce:product_detail_view' product.slug %}">
       <p >{{ product.product_producer }}</p>
       <p >{{ product.name }}</p>
       <!---Changed---->
       {% for category in product.product_category.all  %}
         <p >{{ category.name }}</p>
       {% endfor %}
     </a>
  </div>
{% endfor %}
  • Related