Django模型错误:代码未呈现

3z6pesqy  于 12个月前  发布在  Go
关注(0)|答案(1)|浏览(105)

所以,我有一个数据库,其中有html代码,我想呈现,我先给你看模型
model.py

class Badge(models.Model):
    
    name = models.CharField(max_length=255)
    code = models.CharField(max_length=700, default='<h5><span class="badge bg-primary ms-2">New</span></h5>')
    code_price = models.CharField(max_length=700, default='<s>{{product.price | safe}}</s><strong class="ms-2 text-danger">{{product.get_price_with_discount() | safe}}</strong>')
    # product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='badges')
    
    def __str__(self):
        return self.name

class Product(models.Model):
    """A product."""

    name = models.CharField(max_length=255)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    image = models.ImageField(upload_to='static/img/' , default='/static/img/404.png')
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    type = models.ForeignKey(Badge, on_delete=models.CASCADE, related_name= "products")
    
    def get_price_with_discount(self):
        discount = (self.price) * (10 / 100)
        return self.price - discount

现在在Django中,它找不到product.price,因为product是后来定义的,或者你可以说它呈现了它,但我现在以字符串的形式发送html页面代码
services.html

<section>
        <div class="text-center container py-5">
            <h4 class="mt-4 mb-5"><strong>Bestsellers</strong></h4>

            <div class="row">
                {% for product in products %}

                <div class="col-lg-4 col-md-12 mb-4">
                    <div class="card">
                        <div class="bg-image hover-zoom ripple ripple-surface ripple-surface-light"
                            data-mdb-ripple-color="light">
                            <img src="{{product.image.url}}" class="w-100" style="height: 500px;" />
                            <a href="#!">
                                <div class="mask">
                                    <div class="d-flex justify-content-start align-items-end h-100">
                                        {% comment %} <h5><span class="badge bg-primary ms-2">New</span></h5> {%endcomment %}
                                        {{product.type.code|safe}}
                                    </div>
                                </div>
                                <div class="hover-overlay">
                                    <div class="mask" style="background-color: rgba(251, 251, 251, 0.15);"></div>
                                </div>
                            </a>
                        </div>

                        <div class="card-body">
                            <a href="" class="text-reset">
                                <h5 class="card-title mb-3">{{ product.name}}</h5>
                            </a>
                            <a href="" class="text-reset">
                                <p>{{ product.category.name}}</p>
                            </a>
                            <h6 class="mb-3">${{ product.type.code_price | safe }}</h6>
                        </div>

                    </div>
                </div>
            {% endfor %}

        </div>
    </section>

我想要这样的东西

我得到了像这样的

b4lqfgs4

b4lqfgs41#

你并没有把呈现的数据存储在字符串中,你只是把代码存储在字符串中。
但无论如何我不会这样做:你可以从一个字符串中渲染它,所以:

from django.template import Context, Template

class Badge(models.Model):
    # …
    
    code_price = models.CharField(
        max_length=700,
        default='<s>{{product.price|safe}}</s><strong class="ms-2 text-danger">{{product.get_price_with_discount|safe}}</strong>',
    )

class Product(models.Model):
    # …
    
    def get_price_with_discount(self):
        discount = self.price * (10 / 100)
        return self.price - discount

    def render_code_price(self):
        template = Template(self.type.code_price)
        return template.render(Context({'product': self}))

然后在模板中,您可以用途:

{{ product.render_code_price }}

它将使用相关Badgecode_price来完成此操作。
你也不能在Django的模板语言中使用括号,所以是{{ product.get_price_with_discount }},而不是{{ product.get_price_wit_discount() }}。您可能需要更改数据库中现有的徽章。

相关问题