Skip to main content
 首页 » 编程设计

python之我如何知道 Django 中的变量是否包含任何内容

2025年05月04日26davidwang456

我的观点.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> 

有什么方法可以检查 friends 是否真的包含任何东西?因为如果没有,我想显示其他内容(这很明显)

请您参考如下方法:

具有QuerySet的真实性

QuerySet 的真实性 是它是否包含任何元素,因此您可以检查if friends。喜欢:

<div class="panel panel-primary"> 
  <div class="panel-heading"> 
    <h1>Friends</h1> 
  </div> 
  <div class="panel-body"> 
    <b>{% if not friends %}</b> 
    <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 调用更有效,因为它将结果加载为嗯,在单个数据库查询中。

使用 EXISTS 查询

但是,如果您不对使用查询集感兴趣,而只想查看查询集是否至少有一个元素,您可以使用friends.exists()调用。请注意,在 Django 模板中,您不添加方括号:如果元素是可调用的,Django 将自动调用它:

<div class="panel panel-primary"> 
  <div class="panel-heading"> 
    <h1>Friends</h1> 
  </div> 
  <div class="panel-body"> 
    <b>{% if not friends.exists %}</b> 
    <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-空循环

Django 模板也有一个 {% for ...%}-{% empty %}-{% endfor %}构造。可以在 for 循环的 body 下方和 {% endfor %} 之前使用 {% empty %} 模板 包含在 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> 
    <b>{% empty %}</b> 
    <p>You don't have any friends :( Add one!!</p> 
    {% endfor %} 
  </div> 
</div>