我的views.py:
class PostView(TemplateView):
template_name = 'airapp/post_list.html'
def get(self, request):
form = PostForm()
posts = Post.objects.order_by('-date')
users = User.objects.exclude(id=request.user.id)
friend, created = Friend.objects.get_or_create(current_user=request.user)
friends = friend.users.all()
args = {'form' : form, 'posts' : posts, 'users': users, 'friends' : friends}
return render(request, self.template_name, args)
我的post_list.html
<div class="panel panel-primary">
<div class="panel-heading">
<h1>Friends</h1>
</div>
<div class="panel-body">
{% if **not friends.exists()** %}
<p>You don't have any friends :( Add one!!</p>
{% else %}
{% for friend in friends %}
<a href="{% url 'airapp:profile_with_pk' pk=user.pk %}"><h3>{{ friend.username }}</h3></a>
<a href="{% url 'airapp:change_friend' operation='remove' pk=friend.pk %}"><button type="button" class="btn btn-warning" name="button">Remove Friend</button> </a>
{% endfor %}
{% endif %}
</div>
</div>
有什么方法可以检查朋友是否真的包含任何东西?因为如果没有,我想显示其他内容(这很明显)
QuerySet
井感实性的QuerySet
是,它是否包含任何元素,所以你可以检查if friends
。喜欢:
<div class="panel panel-primary">
<div class="panel-heading">
<h1>Friends</h1>
</div>
<div class="panel-body">
{% if not friends %}
<p>You don't have any friends :( Add one!!</p>
{% else %}
{% for friend in friends %}
<a href="{% url 'airapp:profile_with_pk' pk=user.pk %}"><h3>{{ friend.username }}</h3></a>
<a href="{% url 'airapp:change_friend' operation='remove' pk=friend.pk %}"><button type="button" class="btn btn-warning" name="button">Remove Friend</button> </a>
{% endfor %}
{% endif %}
</div>
</div>
如果尚未进行查询,则Django会进行查询,因此,如果您要遍历QuerySet
,则此方法比存在调用更为有效,因为它还会在单个数据库查询中加载结果。
EXISTS
查询但是,如果您对使用查询集不感兴趣,而只想查看查询集是否具有至少一个元素,则可以使用该friends.exists()
调用。请注意,在Django模板,你就不能加括号:如果一个元素是可调用的,Django会自动调用它:
<div class="panel panel-primary">
<div class="panel-heading">
<h1>Friends</h1>
</div>
<div class="panel-body">
{% if not friends.exists %}
<p>You don't have any friends :( Add one!!</p>
{% else %}
{% for friend in friends %}
<a href="{% url 'airapp:profile_with_pk' pk=user.pk %}"><h3>{{ friend.username }}</h3></a>
<a href="{% url 'airapp:change_friend' operation='remove' pk=friend.pk %}"><button type="button" class="btn btn-warning" name="button">Remove Friend</button> </a>
{% endfor %}
{% endif %}
</div>
</div>
(在您进行遍历时,通常需要第二个查询,在这种情况下)。
for
-empty
循环Django模板也有一个{% for ...%}
- {% empty %}
-{% endfor %}
结构。可以在循环主体{% empty %}
下方和循环主体for
之前使用模板,该模板{% endfor %}
包含在for
循环中没有元素的情况下显示的内容,例如:
<div class="panel panel-primary">
<div class="panel-heading">
<h1>Friends</h1>
</div>
<div class="panel-body">
{% for friend in friends %}
<a href="{% url 'airapp:profile_with_pk' pk=user.pk %}"><h3>{{ friend.username }}</h3></a>
<a href="{% url 'airapp:change_friend' operation='remove' pk=friend.pk %}"><button type="button" class="btn btn-warning" name="button">Remove Friend</button> </a>
{% empty %}
<p>You don't have any friends :( Add one!!</p>
{% endfor %}
</div>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句