如何过滤查询视图.py并将它们呈现到模板中?

2024-10-16 20:53:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我从一个基本的角度理解查询。如果我写下:

{% for item in items %}
    {{ item.jurisdiction}}
{% endfor %}

它将为数据库表中的每一个在“辖区”列中有一个条目的行项捕获该行并只返回“辖区”。在

我真正想做的是返回过滤后的东西。据我所知,后端是查询/过滤的地方。对我来说,这意味着在视图.py在

现在,我真正想做的是通过多个参数进行筛选。”例如,管辖权“+”保留规则“。在

我正在寻找如何在中创建筛选查询的说明视图.py然后在索引.html(或其他模板)。另外,想知道谁来执行可以在模板页面中使用的多种不同类型的查询。在

视图.py

^{pr2}$

“test”不呈现也不调用。但是,我只是把它放在那里,以帮助我思考如何创建可以在模板中访问的不同类型的过滤查询。在

模型.py

from django.db import models

class Item(models.Model):
    id = models.AutoField(primary_key=True)
    jurisdiction = models.CharField(max_length=200)
    business_function = models.CharField(max_length=200)
    record_category = models.CharField(max_length=200)
    record_type = models.CharField(max_length=200)
    retention_rule = models.DecimalField(decimal_places=1,max_digits=3)
    retention_trigger = models.CharField(max_length=200)
    personal_data = models.BooleanField(default=True)
    privacy_driven = models.BooleanField(default=True)

索引.html

{% for item in items %}

    <div class="row">
        <div class="col-md-12">
            <h1>{{ item.jurisdiction }}</h1>
            <h1>{{ item.record_category }}</h1>
            <h1>{{ item.record_type }}</h1>
            <h1>{{ item.retention_rule }}</h1>
            <h1>{{ item.retention_trigger }}</h1>
            <h1>{{ item.personal_data }}</h1>
            <h1>{{ item.privacy_driven }}</h1>
        </div>
    </div>

{% endfor %}

如有任何建议,我们将不胜感激。在


Tags: pydiv视图模板truemodelsrecorditem
1条回答
网友
1楼 · 发布于 2024-10-16 20:53:13

认为视图函数只是另一个普通函数。测试未呈现,因为函数已返回。一旦函数返回任何内容,就不再执行其他行。在

通过传递上下文中的所有查询集,可以将多个筛选的查询传递给模板。在

return render(request, 'inventory/index.html', {'items': items, 'test': test})

相关问题 更多 >