我怎么知道Django中的变量是否包含任何内容?

时间:2018-06-16 16:53:56

标签: python django

我的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>

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

2 个答案:

答案 0 :(得分:4)

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模板中,您执行 not 添加括号:如果元素可调用,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>模板} loop没有eleemnts,例如:

a = np.array ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b = np.array ([0, 1, 2, 3, 4, 5, 6, 7, 10, 10])
np.mean (a! = b)

答案 1 :(得分:1)

<div class="panel panel-primary">
  <div class="panel-heading">
    <h1>Friends</h1>
  </div>
  <div class="panel-body">
    {% if friends %}
    {% 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 %}
    {% else %}
    <p>You don't have any friends :( Add one!!</p>
    {% endif %}
  </div>
</div>