检查django空表对象无法正常工作

时间:2016-08-15 22:11:13

标签: python html django

我是一个html页面,我只有在传入的表格对象不为空时才显示搜索栏。但我的检查工作不正常。这是代码:

<!-- We'll display the search bar only when the user has access to at least one item, otherwise, hide it. -->
{% if item_info %}
Number of entries: {{ item_info|length }}, nothing? {{item_info}}
<section>
    <form method="post" action=".">
      {% csrf_token %}
      <input type="text" class="search-query span80" id="search" name="search" placeholder="Enter ItemNo to search">
      <button type="submit" class="btn">Search</button> 
    </form>
</section>
{% else %}
No item_info.
{% endif%}

这是我在浏览器上看到的内容: still enters if branch

item_info是空白的,我认为它应该转到其他分支,但是,它输入如果分支,任何帮助非常感谢!

在elethan的回答后编辑: 我打印出来进行调试,这是截图: 2nd_img 所以,看起来这个item_info真的是空的,我没有看到任何item_info对象被打印出来。

另外,为了帮助调试,请查看我的观看代码:

def item_info(request):    
    iteminfo= ItemInfo.objects.all().filter(Q(some_query)
    table = ItemInfoTable(iteminfo)
    RequestConfig(request).configure(table)
    return render(request, 'item_info.html', {'item_info':table,})

这是我的表定义:

import django_tables2 as tables  
class ItemInfoTable(tables.Table):
    itmno = tables.Column(verbose_name="Item #")
    class Meta:
        model = ItemInfo
        empty_text = "There is no item record." 

以下是它引用的ItemInfo表:

class ItemInfo(models.Model):
    itmno = models.CharField(primary_key=True, max_length=11L, db_column='ItmNo', blank=True)
    class Meta:
        db_table = 'item_info'

1 个答案:

答案 0 :(得分:2)

如果RawQuerySet{% if item_info.all %},请尝试{% if item_info %}而不是RawQuerySet__bool__()未定义True方法,因此实例始终被视为import time from multiprocessing import Pool def process_frame(frame_id, frame_data): # this function simulates the processing of the frame. # I used a longer sleep thinking that it takes longer # and therefore the reason of parallel processing. print("..... got frame {}".format(frame_id)) time.sleep(.5) char = frame_data[frame_id] count = frame_data.count(char) return frame_id, char, count def process_result(res): # this function simulates the function that would receive # the result from analyzing the frame, and do what is # appropriate, like printing, making a dict, saving to file, etc. # this function is called back when the result is ready. frame_id, char, count = res print("in frame {}".format(frame_id), \ ", character '{}' appears {} times.".format( chr(char), count)) if __name__ == '__main__': pool = Pool(4) # in my laptop I got these times: # workers, time # 1 10.14 # 2 5.22 # 4 2.91 # 8 2.61 # no further improvement after 4 workers. # your case may be different though. from datetime import datetime as dt t0 = dt.now() for i in range(20): # I limited this loop to simulate 20 frames # but it could be a continuous stream, # that when finishes should execute the # close() and join() methods to finish # gathering all the results. # The following lines simulate the video streaming and # your selecting the frames that you need to analyze and # send to the function process_frame. time.sleep(0.1) frame_id = i frame_data = b'a bunch of binary data representing your frame' pool.apply_async( process_frame, #func (frame_id, frame_data), #args callback=process_result #return value ) pool.close() pool.join() print(dt.now() - t0) 。请参阅以下重复的文档的this section中的警告,以防万一此链接将来死亡:

  

虽然RawQuerySet实例可以像普通法一样迭代   QuerySet,RawQuerySet不实现您可以使用的所有方法   查询集。例如,未定义 bool ()和 len ()   RawQuerySet,因此所有RawQuerySet实例都被认为是True。   这些方法没有在RawQuerySet中实现的原因是   在没有内部缓存的情况下实现它们将是一种表现   缺点和添加这样的缓存将是向后不兼容的。