Django奇数布尔巴哈维奥

Django奇数布尔巴哈维奥,django,Django,我有一个布尔字段,用于判断项目是否处于活动状态: is_active = models.BooleanField(default=True) 看起来很简单,我的模板将显示活动的项目: {% for p in products|dictsortreversed:"id" %} {% if p.is_active %} <a href="{{ p.get_absolute_url }}"> {{ p.name }} </a> {% endi

我有一个布尔字段,用于判断项目是否处于活动状态:

is_active = models.BooleanField(default=True)
看起来很简单,我的模板将显示活动的项目:

{% for p in products|dictsortreversed:"id" %}
    {% if p.is_active %}
    <a href="{{ p.get_absolute_url }}">
    {{ p.name }} 
    </a>
{% endif %}
视图:


如果将
{{p.is\u active}}
放在
{%if%}
标记之前,会得到什么?我假设
{{if.is\u active==True}}
起作用,这是一种模板语言与Python不匹配的情况?就个人而言,我只会通过上下文传递您想要的对象。在视图中处理逻辑更为典型。@marksweb既不{如果p.is_active==True}也不{如果p.is_active==False}返回任何值。在上下文中进行过滤是有意义的,不管实际值是0还是1,{p.is_active}}在{%if%}标记之前,{%if%}总是检查管理布尔值,这仍然很奇怪。您可以发布您的模型和视图吗?如果可以的话,我会在早上回来检查:)我想看看for循环上的过滤器是否也是问题所在。如果只是
{%p在产品%}
中,会发生什么。也许过滤器让你失望了?
class Product(models.Model):
    name = models.CharField(max_length=255, unique=True)
    slug = models.SlugField(max_length=255, unique=True, help_text='Unique value for product page URL, created from name')
    price = models.DecimalField(max_digits=9, decimal_places=2, blank=True, default=0.00)
    old_price = models.DecimalField(max_digits=9, decimal_places=2, blank=True, default=0.00)
    image = models.CharField(max_length=50)
    is_active = models.BooleanField(default=True)
    quantity = models.IntegerField()
    description = models.TextField()
    meta_keywords = models.CharField('Meta Keywords', max_length=255, help_text='Comma-delimited set of SEO keywords for meta tag')
    meta_description = models.CharField('Meta Description', max_length=255, help_text='Content for description meta tag')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    categories = models.ForeignKey(Category, null=True)
    publish_date = models.DateField(blank=True, null=True)
    issue_one = models.CharField(blank=True, null=True, max_length=255)
    issue_two = models.CharField(blank=True, null=True, max_length=255)
    issue_three = models.CharField(blank=True, null=True, max_length=255)

    class Meta:
        db_table = 'products'
        ordering = ['-created_at']

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ('catalog_product', (), {'product_slug': self.slug})
def index(request, template_name="catalog/index.html"):
    """ site home page """
    page_title = 'Visible Language Ordering'
    return render_to_response(template_name, locals(), context_instance=RequestContext(request))