使用其他模板中的一些标签和变量到不同的
Use some tags and variables from other template to a different
我根据这个模型设计了一个类别列表(实际上我只创建了两个类别):
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 %}
我的问题是,我想在位于名为 "sample.html" 的同一目录中的第二个模板中列出相同的类别,我认为在第一个实例中只放置相同的第一个模板中使用的标签 "sample.html":
{% for supercategory in object_list %}
<li>
<a href="#">{{ supercategory.name }}</a>
</li>
{% endfor %}
但没有显示任何内容。我应该更改什么才能在两个模板中列出我的类别?
我是 django 的新手,也是 python 的新手,如果我忽略了什么,请提前致歉。
谢谢!!
您是否创建了第二个视图以与 sample.html
模板一起使用?
class SampleCatView(SuperCatView):
template = 'products/sample.html'
您可以继承第一个视图并覆盖模板:-)
我根据这个模型设计了一个类别列表(实际上我只创建了两个类别):
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 %}
我的问题是,我想在位于名为 "sample.html" 的同一目录中的第二个模板中列出相同的类别,我认为在第一个实例中只放置相同的第一个模板中使用的标签 "sample.html":
{% for supercategory in object_list %}
<li>
<a href="#">{{ supercategory.name }}</a>
</li>
{% endfor %}
但没有显示任何内容。我应该更改什么才能在两个模板中列出我的类别?
我是 django 的新手,也是 python 的新手,如果我忽略了什么,请提前致歉。
谢谢!!
您是否创建了第二个视图以与 sample.html
模板一起使用?
class SampleCatView(SuperCatView):
template = 'products/sample.html'
您可以继承第一个视图并覆盖模板:-)