如何在渲染页面之前过滤?

时间:2020-10-19 09:11:05

标签: python django filter view

我想通过扇区过滤一些对象。

这是我的模特:

class Videos(models.Model):
    brand = models.CharField(max_length=255, verbose_name="Brand Name", blank=False, null=True)
    sector = models.CharField(max_length=255, verbose_name="Sector Name", blank=False, null=True)
    video = models.FileField(upload_to="videos/")

这是我要对其进行过滤的视图:

def automotive(request):
    videos = Videos.objects.filter()

问题在我的导航栏中,我只有几个部门名称。用户单击一个时,它应使用用户单击的扇区名称来渲染视频。 例如,我的部门名称之一是“汽车”。因此,当我单击该按钮时,我应该看到只有扇区是汽车的视频。

对不起,我的英语不好,我竭尽全力...

1 个答案:

答案 0 :(得分:0)

因此,我在模型中添加了自己的行业选择;

<div class="col align-self-end pr-0">
      <div class="card card-body justify-content-center" style="height:150px">

        <h5 class="card-title">Privacy & Security</h5>

        <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>

      </div>

  </div>

我给了像这样的视图过滤参数;

sectors = (
    ("FMCG", "FMCG"),
    ("Retail", "Retail"),
    ("E-Commerce", "E-Commerce")
)

有效。