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 %}
'''

猜你喜欢

转载自blog.csdn.net/qq_39974381/article/details/80826995
今日推荐