如何在使用Django开发Web前端时插入count()值

huangapple go评论98阅读模式
英文:

How to insert count() value when using Django to develop Web front end

问题

  1. <th>{{ obj.age }}</th>
  1. # itsec_china.objects.create(cerfitied_product='Game Security App',
  2. # company = 'riot',
  3. # cerfitication_number = 'xxx111',
  4. # certification_date = 20220214,
  5. # validation_date = 20240214)
  6. TryCC.objects.create(name='Bruce',
  7. major='CS',
  8. age= 27)
  1. SELECT COUNT(Issue_date) AS NumberOfProducts FROM first_project.itsec_china WHERE Issue_date like '2022%';
英文:

I'm developing a web visual board using Django.

Here is the part of html that I want to insert database's values into.

  1. <tbody>
  2. {% for obj in data_list %}
  3. <tr>
  4. <th>{{ obj.id }}</th>
  5. <th>{{ obj.major }}</th>
  6. <th>{{ obj.name }}</th>
  7. <th>{{ obj.age }}</th>
  8. </tr>
  9. {% endfor %}
  10. </tbody>

As you can see, this can help me to insert data: id, major, name and age.
But now I want to insert values that use count() function in MySQL, which is fuzzy lookup:
SELECT COUNT(Issue_date) AS NumberOfProducts FROM first_project.itsec_china WHERE Issue_date like '2022%';

Basically, I want to insert the number of those items issued in year 2022.
How can I change my code, for example, this part:
{{ obj.age }}

Below is what is in my view.py:

  1. def orm(request):
  2. # Insert data to database
  3. # itsec_china.objects.create(cerfitied_product='Game Security App',
  4. # company = 'riot',
  5. # cerfitication_number = 'xxx111',
  6. # certification_date = 20220214,
  7. # validation_date = 20240214)
  8. TryCC.objects.create(name='Bruce',
  9. major='CS',
  10. age= 27)
  11. data_list = TryCC.objects.all()
  12. return render(request, 'cc_list.html', {'data_list': data_list})

I'm trying to find someone have the knowledge to give me the answer or a place to study about it. Thank you in advance.

答案1

得分: 0

  1. 你应该在你的视图文件中添加以下行

count = itsec_china.objects.filter(Issue_date__startswith='2022').count()

....
return render(request, 'cc_list.html', {'data_list': data_list, 'count': count})

  1. 然后你的模板应该如下所示

<tbody>
{% for obj in data_list %}
<tr>
<th>{{ obj.id }}</th>
<th>{{ obj.major }}</th>
<th>{{ obj.name }}</th>
<th>{{ obj.age }}</th>
</tr>
{% endfor %}
<tr>
<th>Count</th>
<th></th>
<th></th>
<th>{{ count }}</th>
</tr>
</tbody>

  1. 简而言之,`filter` 方法用于获取 2022 年发布的项目数量。`Issue_date__startswith` 查找用于筛选 Issue_date 字段以 '2022' 开头的行。然后使用 `count` 方法计算匹配筛选条件的行数。
英文:

You should add the following line under your view file

  1. count = itsec_china.objects.filter(Issue_date__startswith=&#39;2022&#39;).count()
  2. ....
  3. return render(request, &#39;cc_list.html&#39;, {&#39;data_list&#39;: data_list, &#39;count&#39;: count})

and then your template should look like

  1. &lt;tbody&gt;
  2. {% for obj in data_list %}
  3. &lt;tr&gt;
  4. &lt;th&gt;{{ obj.id }}&lt;/th&gt;
  5. &lt;th&gt;{{ obj.major }}&lt;/th&gt;
  6. &lt;th&gt;{{ obj.name }}&lt;/th&gt;
  7. &lt;th&gt;{{ obj.age }}&lt;/th&gt;
  8. &lt;/tr&gt;
  9. {% endfor %}
  10. &lt;tr&gt;
  11. &lt;th&gt;Count&lt;/th&gt;
  12. &lt;th&gt;&lt;/th&gt;
  13. &lt;th&gt;&lt;/th&gt;
  14. &lt;th&gt;{{ count }}&lt;/th&gt;
  15. &lt;/tr&gt;
  16. &lt;/tbody&gt;

In a nutshell, the filter method is used to get the count of items issued in 2022. The Issue_date__startswith lookup is used to filter the rows where the Issue_date field starts with '2022'. The count method is then used to count the number of rows that match the filter.

huangapple
  • 本文由 发表于 2023年2月16日 16:10:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定