繁体   English   中英

Django - 如何在模板中获取长度和计数命令? (模板中的查询集)

[英]Django - how to get length and count commands in template? (query-set in template)

我有这些模块:Django 中的 Sales、Gender 和 Type。

表格1:

class Sales(models.Model):
    sname = models.CharField(max_length=100, default="-")
    sgender = models.ForeignKey(MainGender, on_delete=models.CASCADE)
    stype = models.ForeignKey(MainTypes, on_delete=models.CASCADE)
    sdate = models.TextField(default="-")   

    +------+-----+------+------+
    | name |gende| type | date |
    +------+-----+------+------+
    | A    | 1   | 1    | 2019 |
    +------+-----+------+------+
    | B    | 2   | 1    | 2018 |
    +------+-----+------+------+
    | A    | 1   | 3    | 2019 |
    +------+-----+------+------+
    | C    | 2   | 3    | 2017 |
    +------+-----+------+------+
    | A    | 1   | 2    | 2019 |
    +------+-----+------+------+

表2(重点)

class MainGender(models.Model):
    mid = models.CharField(max_length=25, default="-")
    mgender = models.CharField(max_length=25, default="-")

+----+--------+
| id | gender |
+----+--------+
| 1  | Male   |
+----+--------+
| 2  | Female |
+----+--------+
| 3  | -      |
+----+--------+

表 3(重点)

class MainTypes(models.Model):
    tid = models.CharField(max_length=50, default="-")
    ttype = models.CharField(max_length=50, default="-")

+----+------+
| id | type |
+----+------+
| 1  | U1   |
+----+------+
| 2  | X2   |
+----+------+
| 3  | B1   |
+----+------+
| 4  | H3   |
+----+------+

然后,我想计算主表中的所有内容并将它们显示在模板中,如下所示

+---------------+---+
| Total records | 5 |
+---------------+---+
| Male          | 3 |
+---------------+---+
| Female        | 2 |
+---------------+---+
| Type U1       | 2 |
+---------------+---+
| Type X2       | 1 |
+---------------+---+
| Type B1       | 2 |
+---------------+---+
| Type H3       | 0 |
+---------------+---+

我只能通过下面的代码获取模板中的总记录

在views.py中

def Sale_p(request):
    saless = Sale.objects.all()
    return render(request, 'sale.html', {'sales':saless})

我在模板 (sale.html) 中尝试了这些代码

{{ sales|length }} (gives you total records in Table 1, in this example 5)

{{ sales.gender|length }} (gives nothing)

{{ sales.type|length }} (gives nothing) 

{{ sales.gender_set|length }} (gives nothing)

{{ sales.type_set|length }} (gives nothing)

{{ sales.gender(1)|length }} (gives nothing)

{{ sales(gender='1')|length }} (gives nothing)

{{ sales.gender.count() }} (gives nothing)

他们没有在模板中工作。 我想在记录中看到我们有多少次“男性”或“女性”或类型等等......

在模板中,sale.html

+---------------+--------------------+
| Total records | {{ sales|length }} |
+---------------+--------------------+
| Male          | ?                  |
+---------------+--------------------+
| Female        | ?                  |
+---------------+--------------------+
| Type U1       | ?                  |
+---------------+--------------------+
| Type X2       | ?                  |
+---------------+--------------------+
| Type B1       | ?                  |
+---------------+--------------------+
| Type H3       | ?                  |
+---------------+--------------------+

为他人获取结果的代码应该是什么? 或者我应该在 views.py 中使用不同的方式?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM