我是Django的新手,我想知道如何创建一种过滤器,以便只有一个对象符合其中一个属性的条件才会出现。我尝试在模板中使用标签,但它不起作用,我不知道它是否是最有效的方式。例如,我希望在我的表中看到属性“asignar”具有特定值的对象,例如文本“Pedro”。我愿意接受不一定适用于标签的建议。
models.朋友:
class reporte_fallo(models.Model):
num_inventario = models.CharField(max_length=25)
serie = models.CharField(max_length=25)
falla = models.TextField()
descripcion = models.TextField()
LOAN_STATUS = (
('m', 'Maintenance'),
('o', 'On loan'),
('a', 'Available'),
('r', 'Reserved'),
)
area = models.CharField(max_length=1,choices=LOAN_STATUS,blank=True)
fecha = models.DateField(auto_now=False, auto_now_add=True)
asignar = models.CharField(max_length=20, default=True)
fecha_comp = models.DateField(default=timezone.now)
view.朋友:
class reportelist(ListView):
model = reporte_fallo
template_name = 'formulario/reporte_list_jefe.html'
Template.html(这是我的模板,在这种情况下使用标签不起作用的部分“and report.assign ==”Pedro“on if,它只是发送消息”列表为空“)
<tbody>
{% if object_list and reporte.asignar == "Pedro" %}
{% for reporte in object_list %}
<tr>
<td>{{reporte.id}}</td>
<td></td>
<td>{{reporte.falla}}</td>
<td>{{reporte.fecha}}</td>
<td>{{reporte.area}}</td>
<td>{{reporte.asignar}}</td>
<td>{{reporte.fecha_comp}}</td>
<td></td>
<td>
<a href="{% url 'formulario:reporte_editar' reporte.id %}">Asignar</a>
<a href="{% url 'formulario:reporte_eliminar' reporte.id %}">Eliminar</a>
</td>
</tr>
{% endfor %}
{% else %}
<h1>The list is empty</h1>
{% endif%}
</tbody>
我希望你的回答非常感谢!
在模板中,qazxsw poi条件应该在qazxsw poi循环内移动。有点像:
if
如果您只想查看{% for %}
对象,可能值得覆盖{% for reporte in object_list %}
{% if reporte.asignar == "Pedro" %}
<tr>
<td>{{reporte.id}}</td>
</tr>
{% endif %}
{% endfor %}
:
reporte.asignar == "Pedro"
然后没有必要检查模板:
queryset in the view