luofeiyu_cu 发表于 2014-08-18 20:21

format 函数没有看懂

from flask import Flask, request, redirect, url_for

app = Flask(__name__)

@app.route('/')
def index():
    return redirect(url_for('username'), code=302)    # URL跳转,默认代码是302,可以省略

@app.route('/username', methods=['GET', 'POST'])
def username():
    HTML = '''<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Flask POST方法演示</title>
</head>
<body>
{}
<form action="" method="POST">
    <input type="text" name="username" value="" />
    <input type="submit" name="enter" value="enter" />
</form>

</body>
</html>'''
    if request.method == 'GET':
      return HTML.format('')
    elif request.method == 'POST':
      if request.form['username']:
            return HTML.format('<p>Your name is <strong>{}</strong></p>'.format(request.form['username']))
      else:
            return redirect(url_for('username'))

if __name__ == '__main__':
    app.run(debug=True, host='127.0.0.1', port=8080)

这段代码唯一不懂的地方是:HTML.format('<p>Your name is <strong>{}</strong></p>'.format(request.form['username']))


s='haha'
>>> s.format('<p>Your name is <strong>{}</strong></p>'.format('username'))
'haha'

请问,HTML.format('<p>Your name is <strong>{}</strong></p>'.format(request.form['username']))    究竟是要干什么?

ssfjhh 发表于 2014-08-20 21:04

Fancier Output Formatting
页: [1]
查看完整版本: format 函数没有看懂