from flask import Flask,render_template
app = Flask(__name__)
app.debug = True
@app.route('/')
def hello_world():
context = {
'username':'laonanhai'
}
return render_template('if_used.html',**context)
if __name__ == '__main__':
app.run()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>if模板使用</title>
</head>
<body>
<h1>if逻辑的使用,在模板中怎么使用</h1>
<!--jinja2模板的if语句几乎和python中的一样-->
{% if username=='laonanhai' %}
<p>老男孩是狗比</p>
{% else %}
<p>老男孩猪狗不如</p>
{% endif %}
{% if username == 'goubi' %}
<p>老男孩是狗比</p>
{% elif username == 'laonanhai' %}
<p>老男孩猪狗不如</p>
{% endif %}
</body>
</html>
![]()