from flask import Flask, render_template
app = Flask(__name__)
@app.route('/hello')
@app.route('/hello/<name>')
def hello(name=None):
return render_template('hello.html', name=name)
if __name__ == '__main__':
app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello Sample</title>
</head>
<body>
{% if name %}
<h1>Hello {{ name }} !</h1>
{% else %}
<h1>Hello World!</h1>
{% endif %}
</body>
</html>
{% if name and name == 'admin' %}
<h1>This is admin console</h1>
{% elif name %}
<h1>Welcome {{ name }}!</h1>
{% else %}
<h1>Please login</h1>
{% endif %}
def hello(name=None):
return render_template('hello.html', name=name, digits=[1,2,3,4,5],
users=[{'name':'John'},
{'name':'Tom', 'hidden':True},
{'name':'Lisa'}
{'name':'Bob'}])
{% if name and name == 'admin' %}
<h1>Helle admin</h1>
{% elif name %}
<h1>"Hello" ~ {{ name }} ~ "!"</h1>
{% else %}
<h1>Hello World!</h1>
{% endif %}
{% for digit in digits %}
{{ digit }}
{% endfor %}
{% for digit in digits -%}
{{ digit }}
{%- endfor %}
<dl>
{% for user in users if not user.hidden %}
{% if loop.first %}
<div>User List:</div>
{% endif %}
<div class="{{ loop.cycle('odd', 'even') }}">
<dt>User No. {{ loop.index }}</dt>
<dd>{{ user.name }}</dd>
</div>
{% if loop.last %}
<dir>Total Users: {{ loop.length }}</dir>
{% endif %}
{% else %}
<li>No users found</li>
{% endfor %}
</dl>
<style type="text/css">
.odd {
background-color: #BDF;
}
</style>
|
1
2
3
4
5
6
7
8
9
10
11
12
|
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/hello')
@app.route('/hello/<name>')
def hello(name=None):
return render_template('hello.html', name=name)
if __name__ == '__main__':
app.run(debug=True)
|
{% raw %}
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endraw %}
@app.route('/hello')
@app.route('/hello/<name>')
def hello(name=None):
if name is None:
name = '<em>World</em>'
return render_template('hello.html', name=name)
{% autoescape false %}
<h1>Hello {{ name }}!</h1>
{% endautoescape %}
{% set items = [[1,2],[3,4,5]] %}
{% with foo = 1 %}
{% set bar = 2 %}
{{ foo + bar }}
{% endwith %}
{# foo and bar are not visible here #}
{% with arr = ['Sunny'] %}
{{ arr.append('Rainy') }}
{{ arr }}
{% endwith %}
{% with arr = ['Sunny'] %}
{% do arr.append('Rainy') %}
{{ arr }}
{% endwith %}
<p>{{ request.url }}</p>
@app.route('/')
def index():
session['user'] = 'guest'
return render_template('hello.html')
app.secret_key = '123456'
<p>User: {{ session.user }}</p>
@app.route('/')
def index():
g.db = 'mysql'
return render_template('hello.html')
<p>DB: {{ g.db }}</p>
<p>Host: {{ config.DEBUG }}</p>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
from flask import current_app @app.context_processor def appinfo(): return dict(appname=current_app.name)
<p>Current App is: {{ appname }}</p>
import time
@app.context_processor
def get_current_time():
def get_time(timeFormat="%b %d, %Y - %H:%M:%S"):
return time.strftime(timeFormat)
return dict(current_time=get_time)
<p>Current Time is: {{ current_time() }}</p>
<p>Current Day is: {{ current_time("%Y-%m-%d") }}</p>
from flask import Flask, render_template, session, g, current_app
import time
app = Flask(__name__)
@app.route('/')
def index():
session['user'] = 'guest'
g.db = 'mysql'
return render_template('hello-2.html')
@app.context_processor
def appinfo():
return dict(appname=current_app.name)
@app.context_processor
def get_current_time():
def get_time(timeFormat="%b %d, %Y - %H:%M:%S"):
return time.strftime(timeFormat)
return dict(current_time=get_time)
app.secret_key = '123456'
if __name__ == '__main__':
app.run(debug=True)
<!doctype html>
<title>Hello Sample</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<h1>Hello World!</h1>
<p>Request URL: {{ request.url }}</p>
<p>User: {{ session.user }}</p>
<p>DB: {{ g.db }}</p>
<p>Host: {{ config.DEBUG }}</p>
<p>Current App is: {{ appname }}</p>
<p>Current Time is: {{ current_time() }}</p>
<p>Current Day is: {{ current_time("%Y-%m-%d") }}</p>
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
Flash Message: {{ message }}</li>
{% endfor %}
{% endif %}
{% endwith %}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有