【问题标题】:Django template tag "if" statement not evaluating correctly trying a simple searchDjango 模板标签“if”语句未正确评估尝试简单搜索
【发布时间】:2012-09-12 04:44:47
【问题描述】:

因此,在我的网站上,我希望人们可以搜索已注册帐户的人。 此代码的目标是尝试对所有匹配的用户名、名字和姓氏进行排序。 问题是,我的 if 语句似乎不会评估它们在我的模板中应该采用的方式。例如:

我想首先评估 q 是否与用户名、名字和姓氏匹配,

然后评估 q= username and last_name 而不是 first_name,

然后 q= username=first_name 而不是 last_name,

那么 q=username 而不是 first_name 或 last_name,

那么 q= first_name 和 last_name 而不是用户名,

然后 q=last_name 但不是用户名或名字,

最后是 q=first_name 而不是 username 或 last_name。

我创建了几个测试用户,包括具有以下信息的用户:

username=testuser1, first_name=Jason, last_name=Smith 用户名=新人,名字=坦率,姓氏=testuser2

所以如果我进行搜索,例如“testuser”,结果会显示

用户名:testuser1 姓名:Jason Smith 它会声明“testuser”,q,在用户名、名字和姓氏中。我创建的另一个用户“newperson”也没有出现在搜索结果下。但是,如果我搜索 tesuser2,就会出现 newperson, 但它会声明“testuser2”是名字,当我将它设置为姓氏时。这是我的代码:

#views.py

def search_people(request):
    errors1 = []
    if 'q' in request.GET:
        q = request.GET['q']
        ...
        #Code evlauating errors
        ...
    else:
        usernames = RegisterProfile.objects.filter(user__username__icontains=q)
        last_names = RegisterProfile.objects.filter(last_name__icontains=q)
        first_names = RegisterProfile.objects.filter(first_name__icontains=q)
    ...

#search_results.html

{% if usernames or first_names or last_names %}
    <p>The following members matched your search results:</p>
    {% if usernames and first_names and last_names %}
        {% for username in usernames %}
            <li>Username:{{ username.user.username }} First Name: {{ username.user.get_profile.first_name }} {{ username.user.get_profile.last_name }}</li>
            <li>q is in all three</li>
        {% endfor %}
    {% endif %}
    {% if usernames and last_names and not first_names %}
        {% for username in usernames %}
            <li>Username:{{ username.user.username }} First Name: {{ username.user.get_profile.first_name }} {{ username.user.get_profile.last_name }}</li>
            <li>q is in username and last name</li>
        {% endfor %}
    {% endif %} 
    {% if usernames and first_names and not last_names %}
        {% for username in usernames %}
            <li>Username:{{ username.user.username }} First Name: {{ username.user.get_profile.first_name }} {{ username.user.get_profile.last_name }}</li>
            <li>q is in username and first name</li>
        {% endfor %}
    {% endif %}
    {% if usernames and not first_names %}
        {% if not last_names %}
            {% for username in usernames %}
                <li>Username:{{ username.user.username }} First Name: {{ username.user.get_profile.first_name }} {{ username.user.get_profile.last_name }}</li>
                <li>q is only in username</li>
            {% endfor %}
        {% endif %}
    {% endif %}
    {% if last_names and first_names and not usernames %}
        {% for last_name in last_names %}
            <li>Username:{{ last_name.user.username }} First Name: {{ last_name.user.get_profile.first_name }} {{ last_name.user.get_profile.last_name }}</li>
            <li>q is in first and last name</li>
        {% endfor %}
    {% endif %}
    {% if last_names and not first_names %}
        {% if not usernames %}  
            {% for last_name in last_names %}
                <li>Username:{{ last_name.user.username }} First Name: {{ last_name.user.get_profile.first_name }} {{ last_name.user.get_profile.last_name }}</li>
                <li>q is only in last name</li>
            {% endfor %}
        {% endif %}
    {% endif %}
    {% if first_names and not last_names %}
        {% if not usernames %}  
            {% for first_name in first_names %}
                <li>Username:{{ first_name.user.username }} First Name: {{ first_name.user.get_profile.first_name }} {{ first_name.user.get_profile.last_name }}</li>
                <li>q is only in first name</li>
            {% endfor %}
        {% endif %}
    {% endif %} 
{% endif %} 

我知道这很长,可能不是最好的方法,但我愿意接受任何建议并感谢任何帮助。提前致谢。

【问题讨论】:

  • 我本质上只是希望能够查看搜索输入 q 是否在用户名、名字或姓氏中,然后在我的模板中显示所有匹配的结果,而不是同一用户的任何重复.我不一定关心如何去完成这个,只要它有效。我认为我的解决方案足够简单并且应该可以工作,但是由于未知原因,它无法正常工作。如果有任何混淆,我希望这有助于澄清事情并解释我尝试的解决方案。

标签: django django-templates


【解决方案1】:
matches = RegisterProfile.objects.filter(
     Q(user__username__icontains=q)
    |Q(last_name__icontains=q)
    |Q(first_name__icontains=q)
)

<p>The following members matched your search results:</p>
{% for profile in matches %}
<li>Username: {{ profile.user.username }} Name: {{ profile.first_name }} {{ profile.last_name }}</li>
<li>q is in 
    {% if q in profile.user.username %}username, {% endif %}
    {% if q in profile.first_name %}first name, {% endif %}
    {% if q in profile.last_name %}last name.{%endif%}
</li>
{% endfor %}

这可以清理一些,但构建块在那里。例如,如果 q 只是在用户名中,您将得到类似“q 在用户名中”的行。另外,我假设 RegisterProfile 是您的用户配置文件,所以我省略了 profile.user.get_profile 位。如果不是这种情况,您可能需要重新添加。

我认为您缺少的关键部分是 Q object 的东西。这些允许您在过滤器中执行比“AND”更复杂的事情。

【讨论】:

  • 我阅读了您发布的链接并尝试使用您的建议,但它仍然无法按照我预期的方式工作。例如,我搜索的一些用户没有出现,部分:{% if q in profile.user.username %} 部分总是显示为空白
  • 我试图让它不那么复杂,但实际上我有两种用户类型,它们都扩展了 BaseProfile 模型。我尝试了 student_matches = StudentRegisterProfile ... 和teacher_matches = TeacherRegisterProfile ... 然后我重复了“user_matches”和“broadcaster_matches”而不是“matches”的模板代码,但是如果我在我的模板中输入{{ profile.user_type }} ,即使是注册学生,它也会说“老师”。我知道我的注册是正确的,因为我一直在使用它来区分其他模板中的用户类型。感谢您的意见。还有其他想法吗?
  • 您可能在这里处理的问题不止一个。有很多方法可以处理模型继承,听起来您可能已经使用了具有两个子类和一个类型字段的抽象基来进行配置文件。我认为 {{profile.user_type}} 问题可能是该部门的错误配置。我注意到您正在使用不区分大小写的过滤器,因此 q in profile.user.username 可能会失败,因为它区分大小写。由于 django 模板只会在缺少属性时给你'',所以 profile.user 或 user.username 甚至可能不存在。
  • 我认为此时你最好的选择是一次只关注其中一个,并创建一个导致问题的最小代码示例。我今天看到了一个很好的链接,适用于这里:sscce.org。一旦解决了根本问题,就很容易建立一个简单的示例。如果您最终提出新问题,则应将它们链接到您的原始问题中。
  • 您好,感谢您的帮助。我能够使用 Q 对象弄清楚它。现在效果很好。我第一次尝试时犯了一个愚蠢的错误(即我设置了{'teacher_matches':teacher_matches,'user_matches':teacher_matches})。这就是为什么只会显示教师成绩的原因。 Q 对象工作得很好,也让我的代码更简洁。再次感谢 - 让我头疼不已。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 2016-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多