因此,在我的网站上,我想让人们搜索已注册帐户的人。此代码的目标是尝试对所有匹配的用户名、名字和姓氏进行排序。问题是,我的 if 语句似乎不会评估它们在我的模板中应该采用的方式。例如:
我想首先评估 q 是否与用户名、名字和姓氏匹配,
然后评估如果 q= username 和 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 而不是用户名或姓氏。
我创建了几个测试用户,包括具有以下信息的用户:
用户名=testuser1,名字=杰森,姓氏=史密斯用户名=新人,名字=坦率,姓氏=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 %}
我知道这很长,可能不是最好的方法,但我愿意接受任何建议并感谢任何帮助。提前致谢。