繁体   English   中英

检查查询集中的确切存在 django,jinja 2

[英]check for exact presence in a queryset django, jinja 2

就我而言,我有一个问题要检查查询集中是否存在 model 的确切字符串名称。 这是我的代码:

views.py:

if Despiking.objects.filter(user=request.user).exists():
    filtered_projects = Despiking.objects.filter(user=request.user)
    context.update({
        'filtered_projects': filtered_projects.__str__(),
    })

template.html:

{% if info.project_name in filtered_projects %}
    <!-- some HTML elements -->
{% else %}
    <!-- other HTML elements -->
{% endif %}

在我的代码中, "my project""project"之间没有区别,如info.project_name model。因为当我只有"my project"时, "project"一词存在于查询集中。 所以使用{% if info.project_name in filtered_projects %}的效果相同( if的条件将为True ),因为由于"my project" ,查询集中存在"project"这个词。 我该怎么做才能检查其中的确切字符串?

您可以分别在区分大小写和不区分大小写的操作中使用__exact__iexact进行精确匹配。

分享您的Despiking model,我会编辑答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM