因此,在我的网站上,我希望允许人们搜索已注册帐户的人。 此代码的目标是尝试对所有匹配的用户名,first_names和last_names进行排序。 问题是,我的if语句似乎不会评估它们在我的模板中的应用方式。例如:
我想首先评估q是否匹配username,first_name和last_name,
然后评估q = username和last_name而不是first_name,
然后q = username = first_name而不是last_name,
然后q =用户名而不是first_name或last_name,
然后q = first_name和last_name而不是用户名,
然后q = last_name但不是username或first_name,
最后q = first_name而不是username或last_name。
我创建了几个测试用户,包括具有以下信息的用户:
username = testuser1,first_name = Jason,last_name = Smith username = newperson,first_name = frank,last_name = testuser2
因此,如果我进行搜索,例如“testuser”,结果将显示
用户名:testuser1姓名:Jason Smith 并且它会声明“testuser”,q,在username,first_name和last_name中。我创建的其他用户“newperson”也没有显示在搜索结果下。但是,如果我搜索tesuser2,新人将出现, 但是当我将其设置为姓氏时,它会声明“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 %}
我知道这很长,可能不是最好的方法,但我愿意接受任何建议并感谢任何帮助。提前谢谢。
答案 0 :(得分: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 is in username,”这样的行。另外,我假设RegisterProfile是你的用户配置文件,所以我省略了profile.user.get_profile位。如果不是这种情况,您可能需要重新添加。
我认为你缺少的关键部分是Q object。这些允许您在过滤器中执行比“AND”更复杂的操作。