index.html文件中的if判断句

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p></p>
    {% if user %}
            <a href="#">{{ user.username }}</a>
            <a href="#">注销</a>

    {% else %}
            <a href="#">登陆</a>
            <a href="#">注册</a>
    {% endif %}
</body>
</html>

#encoding:utf-8
from flask import Flask,render_template

app = Flask(__name__)


@app.route('/<is_login>/')
def index(is_login):
    if is_login == '1':
        user = {
        'username':u'龚雪',  #调试时,需要用u进行编码转换,否则访问页面会报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)
        'age':18
         }
        return render_template('index.html',user=user)
    else:
        return render_template('index.html')


if __name__ == '__main__':
    app.run(debug=True)


'''
if判断:
1、index.html中的语法:
    {%if %}
    {%else %}
    {%endif}
2、if的使用,可以和python中的相差无几
3、模板index.html用for循环遍历列表和字典
    1)字典的遍历,语法和Python一样,可以用items(),keys(),values()、iteritems()、iterkeys()、itervalues()
        {% for k,v in 字典名.items() %}
          <p>{{k}}:{{v}}</p>
        {% endfor %}
    2)列表遍历
        websites = ['baidu.com','google.com']
        
        {% for website in websites %}
          <p>{{{website}}</p>
        {% endfor %}
'''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值