使用其他模板中的一些标签和变量到不同的模板

时间:2015-03-27 00:03:10

标签: python django tags django-templates django-views

我设计了一个类别列表(实际上我只创建了两个类别):

class SuperCategory(models.Model):

    name = models.CharField(max_length=50)
    image = models.ImageField(upload_to = 'library', null=True)
    description = models.TextField(null=True)
    slug = models.SlugField(editable=False)

    def save(self, *args, **kwargs):
        if not self.id:
            self.slug = slugify(self.name)
        super(SuperCategory, self).save(*args, **kwargs)

    def __str__(self):
        return self.name

我可以在模板中列出所有类别:

class SuperCatView(ListView):

    template = 'products/supercategory_list.html'
    model = SuperCategory

supercategory_list.html:

{% for supercategory in object_list %}
            <li>
                <img src="{{ supercategory.image.url }}" alt="" />
                <div class="portfolio-item-content">
                    <span class="header">{{ supercategory.name }}</span>
                    <p class="body">{{ supercategory.description }}</p>
                </div>
                <a href="{% url 'products:vista_categories' pk=supercategory.pk %}"><i class="more">+</i></a>
            </li>
            {% endfor %}

我的问题是,我想在名为&#34; sample.html&#34; 的同一目录中的第二个模板中列出相同的类别,我认为在第一个实例中只放了相同的第一个模板中使用的标签为&#34; sample.html&#34;:

{% for supercategory in object_list %}
                        <li>
                            <a href="#">{{ supercategory.name }}</a>
                        </li>
                        {% endfor %}

但没有显示任何内容。我应该更改什么才能在两个模板中列出我的类别?。

我是django的新手,也是python的新手,如果我忽视某些事情,请提前道歉。

谢谢!

1 个答案:

答案 0 :(得分:3)

您是否创建了第二个用于sample.html模板的视图?

class SampleCatView(SuperCatView):
    template = 'products/sample.html'

您可以继承第一个视图,只需覆盖模板: - )

相关问题