Django 模板 if 语句总是计算为 true [英] Django template if statement always evaluates to true

查看:24
本文介绍了Django 模板 if 语句总是计算为 true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来应该很简单,但由于某种原因我无法解决这个问题.我正在使用 Django 1.4.我正在尝试进行基本检查以查看 list QuerySet 在模板呈现期间是否为空,但我使用的 if 语句似乎总是评估为 true.

This seems like it should be pretty straightforward, but for some reason I am unable to solve this problem. I'm using Django 1.4. I am trying to do a basic check to see if a list QuerySet is empty or not during template rendering, but the if statement I'm using seems always to evaluate to true.

我有一个 Django 模板,上面写着:

I have a Django template that reads:

{% extends 'includes/base.html' %}

{% if object_list %}
...
{% block data %}
   {% for object in object_list %}
     ...
     {{ object.create_date }}
     ...
   {% endfor %}
{% endblock data %}
...
{% endif %}

'base.html' 有块:

'base.html' has the block:

<body>
{% block content %}
  ...   
  <div class="row-fluid">
    <div class="span12">
      {% block data %}
      <div align="center"><i>No data.</i></div>
      {% endblock data %}
    </div><!-- span12 -->
  </div><!-- row -->
{% endblock content %}
...
</body>

生成QuerySet的视图函数在这里:

The view function generating the QuerySet is here:

def barcode_track(request, model):
    query = request.GET.get('barcode_search', '')
    object_list = model.objects.all()
    if query:
        object_list = model.objects.filter(barcode__icontains=query)
    return render_to_response('barcode_track/barcode_list.html',
                              {'object_list': object_list, 'query': query},
                              context_instance=RequestContext(request))

通过这种形式调用:

<form id="barcode_search_form" method="get" action="" class="form">
    <input type="text" name="barcode_search" value="{{ query }}" />
    <button type="submit" class="btn">Search</button>
</form>

还有 urls.py 行:

And the urls.py line:

urlpatterns = patterns('barcode_track.views',
                       url(r'^$', 'barcode_track', {'model': Barcode},
                           name="barcode_track"),)

这个想法是,只有在 object_list 中存在结果时才会显示结果,否则父块将保持不变.我已经尝试更改 object_list 的名称,并且我已将 {{ dicts }} 打印到页面以确保 object_list 实际上是,空的(它是).我没有使用通用视图,尽管我意识到这个名字暗示了很多.实际上,我在使用类似逻辑编写的另一个应用程序中遇到了这个问题,所以我一定是在系统地错误地做某事.

The idea is that results will only be presented if they exist in object_list, and otherwise the parent block will remain unaltered. I have tried changing the name of object_list, and I have printed {{ dicts }} to the page to ensure that object_list is, in fact, empty (which it is). I am not using a generic view, although I realize that the name suggests as much. I have actually had this trouble in a different app I wrote using similar logic, so I must be doing something systematically incorrectly.

我在这里错过了什么?

推荐答案

你不能像 if 这样的控制流标签包裹在 block 周围.您的问题是块 data 的子模板定义被使用只是因为它在那里.

You can't wrap control flow tags like if around a block. Your problem is that the child template's definition for block data is being used simply because it's there.

您可以通过在 block data 中放置 if 标签来修复它.如果要在列表为空时继承父级的内容,请添加扩展为 {{ block.super }}else 案例.

You can fix it by placing the if tag inside block data. If you want to inherit the parent's contents when the list is empty, add an else case that expands to {{ block.super }}.

这篇关于Django 模板 if 语句总是计算为 true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆