【问题标题】:Django queryset Product model and Product_images model, TemplateSyntaxErrorDjango queryset Product 模型和 Product_images 模型,TemplateSyntaxError
【发布时间】:2020-12-16 17:08:23
【问题描述】:

我正在使用 Django 开发一个电子商务网站。我有 Product 和 Product_images 模型如下:

class Product(models.Model):
    tags = models.ManyToManyField(Tag, related_name='products')
    same_product = models.ManyToManyField('self', related_name='same_products', blank=True)
    category = models.ForeignKey('Category', on_delete=models.CASCADE, related_name='product_categories')
    who_like = models.ManyToManyField(User, related_name='liked_products', blank=True)
   
    title = models.CharField('Title', max_length=100, db_index=True)
    slug = models.SlugField('Slug', max_length=110, unique = True)
    sku = models.CharField('SKU', max_length=50, db_index=True)
    description = models.TextField('Description', null=True, blank=True)
    sale_count = models.IntegerField('Sale Count', default=0)
    is_new = models.BooleanField('is_new', default=True)
    is_featured = models.BooleanField('is_featured', default=False)
    is_discount = models.BooleanField('is_discount', default=False)
    price = models.DecimalField('Price', max_digits=7, decimal_places=2)
    discount_value = models.IntegerField('Discount Value', null=True, blank=True)

    def __str__(self):
        return self.title
class Product_images(models.Model):
    # relations
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='images')

    # informations
    image = models.ImageField('Image', upload_to='media/product_images')
    is_main = models.BooleanField('Main Image', default=False) 
    is_second_main = models.BooleanField('Second Main Image', default=False) 

    # moderations
    status = models.BooleanField('Status', default=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'image'
        verbose_name = 'Image'
        verbose_name_plural = 'Images'
        ordering = ('created_at',)

    def __str__(self):
        return f'{self.image}'

在我的 Product_images 模型中,我为一个产品存储了多个图像,在我编写的 Product_images 模型中 名称为 is_mainis_second_main 的布尔字段。在我的模板中,我想获取这些图像,在我的终端(交互式外壳)中,当我编写 single_product.images.get(is_main=True).image.url 可以获取图像 url,但在模板中我无法获取图像,但出现如下错误:

TemplateSyntaxError at /
Could not parse the remainder: '(is_main=True).image.url' from 'product.images.get(is_main=True).image.url'

以下是我的视图和模板:

def index(request):
    products = Product.objects.all()

    context = {
        'products': products
    }

    return render(request, 'index/index.html', context)

{% for product in products %}
<div class="front">
   <a href="product-page(no-sidebar).html">
      <img src="{{ product.images.get(is_main=True).image.url }}" class="img-fluid blur-up lazyload bg-img" alt="">
   </a>
</div>
<div class="back">
   <a href="product-page(no-sidebar).html">
      <img src="{{ product.images.get(is_main=True).image.url }}" class="img-fluid blur-up lazyload bg-img" alt="">
   </a>
</div>
{% endfor %}

请帮我解决这个问题并正确显示这些图像,在此先感谢。

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    我已经解决了这个问题,也许这不是最好的方法,但工作。

    {% for product in products %}
       <div class="product-box">
          <div class="img-wrapper">
             <div class="front">
                <a href="{% url 'product-detail' product.slug %}">
                   {% for image in product.images.all %}
                      {% if image.is_main %}
                         <img src="{{ image.image.url }}" class="img-fluid blur-up lazyload bg-img" alt="">
                      {% endif %}
                   {% endfor %}
                </a>
             </div>
          <div class="back">
             <a href="{% url 'product-detail' product.slug %}">
                {% for image in product.images.all %}
                   {% if image.is_second_main %}
                      <img src="{{ image.image.url }}" class="img-fluid blur-up lazyload bg-img" alt="">
                   {% endif %}
                {% endfor %}
             </a>
          </div>
    {% endfor %}
    
    

    【讨论】:

      猜你喜欢
      • 2014-01-13
      • 2016-11-19
      • 1970-01-01
      • 2011-04-18
      • 2014-02-18
      • 2021-11-03
      • 2015-10-26
      • 2020-03-10
      • 2011-07-22
      相关资源
      最近更新 更多