Flask入门-上传的问题

2021-06-20 00:06

阅读:387

YPE >

标签:flask

flask.py

from flask import Flask, render_template, request, redirect, url_for
from werkzeug.utils import secure_filename
from os import path

app = Flask(__name__)


@app.route(‘/‘)
def hello_world():
    return render_template(‘index.html‘, title=‘Welcome‘)


@app.route(‘/services‘)
def services():
    return ‘Servisce‘


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


@app.route(‘/user/‘)
def user(user_id):
    return ‘User %d‘ % user_id


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


@app.route(‘/upload‘, methods=[‘GET‘, ‘POST‘])
def upload():
    if request.method == ‘POST‘:
        file = request.files[‘file‘]
        basepath = path.abspath(path.dirname(__file__))
        upload_path = path.join(basepath, ‘static/uploads‘)
        filename = secure_filename(file.filename)
        file.save(path.join(upload_path,filename))
        return redirect(url_for(‘upload‘))
    return render_template(‘upload.html‘)


if __name__ == ‘__main__‘:
    app.run(debug=True)


upload.html

html>
    
    

文件上传实例

    

                      


本文出自 “joker” 博客,请务必保留此出处http://jokerbug.blog.51cto.com/12943231/1947849

Flask入门-上传的问题

标签:flask

原文地址:http://jokerbug.blog.51cto.com/12943231/1947849


评论


亲,登录后才可以留言!