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

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

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=&#39;2022&#39;).count()

....
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

&lt;tbody&gt;
    {% for obj in data_list %}
    &lt;tr&gt;
        &lt;th&gt;{{ obj.id }}&lt;/th&gt;
        &lt;th&gt;{{ obj.major }}&lt;/th&gt;
        &lt;th&gt;{{ obj.name }}&lt;/th&gt;
        &lt;th&gt;{{ obj.age }}&lt;/th&gt;
    &lt;/tr&gt;
    {% endfor %}
    &lt;tr&gt;
        &lt;th&gt;Count&lt;/th&gt;
        &lt;th&gt;&lt;/th&gt;
        &lt;th&gt;&lt;/th&gt;
        &lt;th&gt;{{ count }}&lt;/th&gt;
    &lt;/tr&gt;
&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:

确定