Чтобы вывести <p>теги, используя теги шаблона
Как я могу использовать тег шаблона для создания "p-тега только для продуктов, которые не имеют product_code в таблице опций"?
{% for option in option_object %}
{{option.product_code}}
{% if option.product_code == None %}}
<p> hello </p>
{% endif %}
{% endfor %}
Я написал это таким образом, но это не работает. Кроме вышеуказанного, мы пробовали также {% if not product.product_code == option.product_code %}, но это не было решено.
Как вы можете только "печатать теги p на продуктах без опций"?
Models.py
class Product(models.Model):
product_code = models.AutoField(primary_key=True)
username = models.ForeignKey(Member, on_delete=models.CASCADE, db_column='username')
category_code = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, related_name='products')
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True, unique=False, allow_unicode=True)
image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
benefit = models.TextField()
detail = models.TextField()
target_price = models.IntegerField()
start_date = models.DateField()
due_date = models.DateField()
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['product_code']
index_together = [['product_code', 'slug']]
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('zeronine:product_detail', args=[self.product_code, self.slug])
class Option(models.Model):
option_code = models.AutoField(primary_key=True)
name = models.CharField(max_length=32)
product_code = models.ForeignKey(Product, on_delete=models.CASCADE, db_column='product_code')
def __str__(self):
return self.name
class Meta:
ordering = ['option_code']
class Value(models.Model):
value_code = models.AutoField(primary_key=True)
option_code = models.ForeignKey(Option, on_delete=models.CASCADE, db_column='option_code', null=True)
product_code = models.ForeignKey(Product, on_delete=models.CASCADE, db_column='product_code', null=True)
name = models.CharField(max_length=32, null=True)
extra_cost = models.IntegerField(null=True)
def __str__(self):
return self.name
class Meta:
ordering = ['value_code']
views.py
def product_detail(request, id, product_slug=None):
current_category = None
categories = Category.objects.all()
product = get_object_or_404(Product, product_code=id, slug=product_slug)
designated_object = Designated.objects.filter(rep_price='True')
element_object = Element.objects.all()
photo_object = Photo.objects.all()
option_object = Option.objects.all()
value_object = Value.objects.all()
return render(request, 'zeronine/detail.html', {'product': product,
'current_category': current_category,
'categories': categories,
'designated_object': designated_object,
'element_object': element_object,
'photo_object':photo_object,
'option_object': option_object,
'value_object': value_object,
})