I have these modules: Sales, Gender and Type in Django.
Table 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 |
+------+-----+------+------+
Table 2 (key)
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 | - |
+----+--------+
Table 3 (key)
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 |
+----+------+
Then, I want to count all the things in the main table and show them in the template as below
+---------------+---+
| Total records | 5 |
+---------------+---+
| Male | 3 |
+---------------+---+
| Female | 2 |
+---------------+---+
| Type U1 | 2 |
+---------------+---+
| Type X2 | 1 |
+---------------+---+
| Type B1 | 2 |
+---------------+---+
| Type H3 | 0 |
+---------------+---+
I can only get total records in template by code below
in views.py
def Sale_p(request):
saless = Sale.objects.all()
return render(request, 'sale.html', {'sales':saless})
I tried these codes in the template (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)
They did not work in template. I want to see in records how many times we had 'male' or 'female' or types and so on...
in template, sale.html
+---------------+--------------------+
| Total records | {{ sales|length }} |
+---------------+--------------------+
| Male | ? |
+---------------+--------------------+
| Female | ? |
+---------------+--------------------+
| Type U1 | ? |
+---------------+--------------------+
| Type X2 | ? |
+---------------+--------------------+
| Type B1 | ? |
+---------------+--------------------+
| Type H3 | ? |
+---------------+--------------------+
What should be the codes to get the results for others? Or should I use different ways in views.py?
Please refer to the link below:
https://docs.djangoproject.com/en/3.0/ref/templates/builtins/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.