2012-07-13 38 views
0

我有一个观点login()合作:如果模板标签不与环境传送布尔

from django.http import HttpRequest 
from useraccounts.models import BadIP 
def login(request): 
    client_address = request.META['REMOTE_ADDR'] 
    client_instance = BadIP.objects.get(ip_address=client_address) 
    if client_instance.ban_state == True: 
     return render(request, 'login.html', {'banned':True}) 
    else: 
     return render(request, 'login.html', {'banned':False}) 

和模板:

{% if banned == False %} 
    <p>Content</p> 
{% endif %} 

{% if banned == True %} 
    <p>Content #2</p> 
{% endif %} 

根据这些标准,无论是两款将在模板中呈现。但是,如果我更改TrueFalse显示为字符串的代码中的实例,突然模板呈现良好。为什么会发生这种情况,并且有什么方法可以将此行为更改为更明显/更正确的语义?

回答

1

它们已经是布尔变量了!无需与他们比较TrueFalse。试试这个:

{% if banned %} 
    <p>Content</p> 
{% else %} 
    <p>Content #2</p> 
{% end if %}