flask快速入门

from flask import Flask,url_for, render_template
from flask import request, redirect

app = Flask(__name__)

# 路由
@app.route(‘/‘)
def index():
    return ‘Index Page‘

# @app.route(‘/hello‘)
# def hello():
#     return ‘Hello World‘

# 变量规则
@app.route(‘/user/<username>‘)
def show_user(username):
    return ‘User %s‘ % username

# url重定向
@app.route(‘/project/‘)
def project():
    return ‘project‘

@app.route(‘/about‘)
def about():
    return ‘about‘


# http方法
# @app.route(‘/login‘, methods=[‘GET‘, ‘POST‘])
# def login():
#     if request.method == ‘POST‘:
#         return ‘do the login‘
#     else:
#         return ‘show the login form‘

# 模板渲染
@app.route(‘/hello/<name>‘)
def hello(name):
    return render_template(‘hello.html‘, name=name)


# 请求对象
@app.route(‘/login‘, methods=[‘GET‘, ‘POST‘])
def login():
    if request.method == ‘POST‘:
        username = request.form[‘username‘]
        password = request.form[‘password‘]
    else:
        return render_template(‘login.html‘)

# 文件上传
@app.route(‘/upload‘, methods=[‘GET‘, ‘POST‘])
def upload_file():
    if request.method == ‘POST‘:
        file_name = request.form[‘filename‘]
        f = request.files[‘the_file‘]
        f.save(‘/home/zhangjian/PycharmProjects/js逆向/app/file/{}‘.format(file_name))
        return render_template(‘upload.html‘)
    else:
        return render_template(‘upload.html‘)


# 重定向和错误
@app.route(‘/bbs‘)
def bbs():
    return redirect(url_for(‘upload_file‘))

if __name__ == ‘__main__‘:
    app.run(‘127.0.0.1‘,5000)