如果用户选择了America,如何显示美国用户的所有帖子(在CharField Choices中)



我正在构建一个BlogApp,我被困在一个问题上。如果request.userCharField

中选择U.S.A,我想显示U.S.A的所有Post有一个名为'location'的属性,它有两个选择'America'和'Australia'。我希望它只显示'当用户选择美国时的美国邮政'。

当用户选择'America'时,我想只显示选择了America的用户的所有帖子。

如果用户选择'Australia',则它查找所有选择'Australia'的用户并显示澳大利亚用户的帖子。

views.py

def showing_post(request,user_id):
profiles = get_object_or_404(Profile,user_id=user_id)
you = request.user
shows = 'none'
show = 'none'
if profiles.location == 'America':
show = Profile.objects.order_by('location')
elif profiles.location == 'Australia':
shows = Profile.objects.order_by('location')
context = {'shows':shows,'show':show,'profiles':profiles}
return render(request, 'opens.html', context)

opens.html

{% if profiles.location == '-------' %}
{% for p in show %}
{{ p.location }}
{% endfor %}
{% endif %}

当我检查页面时,它显示了所有选择美国和澳大利亚位置的用户,所有都显示。

我想做什么

我想要if request.user select America then show only users that have selected America , AND don't show the users of Australia

What have i try

我也试过location__icontains,但没有工作。

我不知道我做错了什么

任何帮助将不胜感激

提前感谢

我也是django的新手,但是你可以试试这个

views.py:

def showing_post(request,user_id):
profile = get_object_or_404(Profile,user_id=user_id)
profile_location = Profile.objects.filter(location=profile.location)
context = {'show':profile_location,'profile':profile}
return render(request, 'opens.html', context)

opens.html:

user:{{ profile.user }}
location:{{ profile.location }}
<h4>Users in {{ profile.location }}:</h4>
{% for p in show %}

{{ p.user }}
{{ p.location }}
{% empty %}
<h4>No other users found in {{ profile.location }}</h3>
{% endfor %}

最新更新