鍍金池/ 問(wèn)答/Python/ django模板if判斷

django模板if判斷

views傳入了一個(gè)boolean類型值pay,明明是False,但是html模板判斷時(shí)依然if pay依然執(zhí)行了語(yǔ)句

                  {% if pay %}
                      <a href="#">pay{{ pay }}</a>
                  {% elif hfut %}
                      <a href="/pay">hfut{{ hfut }}</a>
                  {% elif getin %}
                      <a href="/getin">getin{{ getin }}</a>
                  {% else %}
                      <a href="/login">login</a>
                  {% endif %}

views.py

def index(req):
    username = req.COOKIES.get('username','')
    balance = req.COOKIES.get('balance', '')
    getin = req.COOKIES.get('is_getin','')
    hfut= req.COOKIES.get('hfut', '')
    pay = req.COOKIES.get('pay','')
    return render(req,'lakers.html' ,context={'username':username, 'balance':balance, 'getin':getin, 'hfut':hfut, 'pay':pay})

圖片描述
圖片描述

回答
編輯回答
夕顏

{% if pay == 'False' %}
<!-- other code in here -->
{% endif %}

2017年9月8日 01:13
編輯回答
絯孑氣

你傳了一個(gè)'False'的字符串

2017年9月7日 05:54
編輯回答
莫小染
print(pay, type(pay)) # render之前打印一下,我懷疑你的pay是字符串的'False' python中 if 'False' 是真
return render(req,'lakers.html' ,context={'username':username, 'balance':balance, 'getin':getin, 'hfut':hfut, 'pay':pay})
2017年8月21日 06:30