我有具有下一个结构的自定义插件。models.py 看起来像
class ProductDescription(models.Model):
name = models.CharField(max_length=30)
icon = models.ImageField(upload_to="installation_image", blank=True, null=True)
description = models.TextField(blank=True, null=True)
def __unicode__(self):
return self.name
class ProductPlugin(CMSPlugin):
product = models.ForeignKey(ProductDescription)
class ProductSpecification(models.Model):
product = models.ForeignKey(ProductDescription)
specification = models.TextField(blank=True, null=True)
class InstallationStep(models.Model):
product = models.ForeignKey(ProductDescription)
step = models.TextField(blank=True, null=True)
image = models.ImageField(upload_to="installation_image", blank=True, null=True)
管理员.py
class InstallationStepInline(admin.StackedInline):
model = InstallationStep
extra = 0
class ProductSpecificationInline(admin.StackedInline):
model = ProductSpecification
extra = 0
class DeviceAdmin(admin.ModelAdmin):
inlines = [ProductSpecificationInline ,
InstallationStepInline]
admin.site.register(ProductDescription, DeviceAdmin)
和 cms_plugin.py
class CMSProductPlugin(CMSPluginBase):
model = ProductPlugin
name = _("Product Description")
render_template = "product_description.html"
def render(self, context, instance, placeholder):
context.update({
'product':instance.product,
'object':instance,
'placeholder':placeholder
})
return context
plugin_pool.register_plugin(CMSProductPlugin)
因此,每个产品可能没有或有多个规格和安装步骤。问题是如何在模板中呈现这些规范和步骤?我知道如何获取 ProductDescription 类的数据。它像是
<div class="test">{{ product.description }}</div>
但我怎样才能提取其他数据?我想应该是这样的
{% for steps in product.InstallationStep.all %}
<div class="test">{{ steps.step }}</div>
{% endfor %}
但它什么也没做=(
提前致谢!