英文:
How to insert count() value when using Django to develop Web front end
问题
<th>{{ obj.age }}</th>
# itsec_china.objects.create(cerfitied_product='Game Security App',
# company = 'riot',
# cerfitication_number = 'xxx111',
# certification_date = 20220214,
# validation_date = 20240214)
TryCC.objects.create(name='Bruce',
major='CS',
age= 27)
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.
<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 %}
</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:
def orm(request):
# Insert data to database
# itsec_china.objects.create(cerfitied_product='Game Security App',
# company = 'riot',
# cerfitication_number = 'xxx111',
# certification_date = 20220214,
# validation_date = 20240214)
TryCC.objects.create(name='Bruce',
major='CS',
age= 27)
data_list = TryCC.objects.all()
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
你应该在你的视图文件中添加以下行
count = itsec_china.objects.filter(Issue_date__startswith='2022').count()
....
return render(request, 'cc_list.html', {'data_list': data_list, 'count': count})
然后你的模板应该如下所示
<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>
简而言之,`filter` 方法用于获取 2022 年发布的项目数量。`Issue_date__startswith` 查找用于筛选 Issue_date 字段以 '2022' 开头的行。然后使用 `count` 方法计算匹配筛选条件的行数。
英文:
You should add the following line under your view file
count = itsec_china.objects.filter(Issue_date__startswith='2022').count()
....
return render(request, 'cc_list.html', {'data_list': data_list, 'count': count})
and then your template should look like
<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>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论