在Django中,在引导选项卡中显示带有随机目录名称的选项卡

时间:2020-01-08 09:58:14

标签: django tabs

我对Django Web开发完全陌生。我有三个模型目录,类别,项目。我正在尝试在具有引导程序选项卡的模板中显示它。我已经根据登录用户查询了所有目录。我需要在首页选项卡(活动)中显示第一行,而在其他选项卡中休息。我不想使用django标签,它不适用于我的Django 2.0.7

这是我的模特

class Catalog(models.Model):
    created_date = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated_date = models.DateTimeField(auto_now_add=False, auto_now=True)

    name = models.CharField(max_length=50)
    position = models.IntegerField()
    description = models.TextField()
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.name


class Category(models.Model):
    created_date = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated_date = models.DateTimeField(auto_now_add=False, auto_now=True)

    name = models.CharField(max_length=50)
    position = models.IntegerField()
    description = models.TextField(blank=True)
    catalog_id = models.ForeignKey(Catalog, on_delete=models.CASCADE)

    def __str__(self):
        return self.name


class Item(models.Model):
    created_date = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated_date = models.DateTimeField(auto_now_add=False, auto_now=True)

    name = models.CharField(max_length=50)
    position = models.IntegerField(blank=True)
    description = models.TextField(blank=True)
    category_id = models.ForeignKey(Category, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

在我看来

@login_required
def user_view(request):
    context = {'catalog_list': Catalog.objects.filter(user_id=request.user.id)}
    return render(request, "mydemofolder/userview.html", context,)

在我的模板中

    {% for catalog in catalog_list  %}

           <!-- {% if catalog.name == 'Electronics' %}-->
            <a class="nav-item nav-link active" id="nav-home-tab" data-toggle="tab" href="#nav-home" role="tab" aria-controls="nav-home" aria-selected="true">{{catlog.name}}</a>
           <!-- {% else %}-->
            <a class="nav-item nav-link" id="{{ catalog.name }}" data-toggle="tab" href="#nav-profile" role="tab" aria-controls="nav-profile" aria-selected="false">{{ catalog.name }}</a>

            <!-- {% endif %}-->
    {% endfor %}
</div>
</nav>

0 个答案:

没有答案