Flask学习 官方文档
1.先实现数据库增删改查
2.再配合前端实现管理员的账号登录
3.考虑如何在CentOS上部署 安装python3 CentOS部署flask python3
==11.23———————–再说吧,今天先写会作业,摸了摸了==
设置路由参数 1 2 3 4 5 6 7 @app.route("/user/<user_id>" ) def index (user_id )return “展示的id 为%s ”%user_id @app .route ("/user/int:user_id" ) def index (user_id )return “展示的id 为%s ”%user_id
通过给路由器设置参数,使得客户端可以通过url将参数传递到后端
上传文件 今天醉了,整了半天上传文件没办法,最后是因为这个路径问题:
1 upload_path = os.path.join(basepath, 'static\\uploads' ,secure_filename(f.filename))
这个\\upload
,windows要注意转义符
还有另外一个:
1 2 3 4 5 6 7 8 def create_app (): app = Flask(__name__) return app application=create_app() @application.route('/upload' , methods=['POST' , 'GET' ] ) if __name__ == '__main__' : application.run()
这个避免no module named app问题
完整上传文件代码: app.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 from flask import Flask, render_template, request,redirect,url_forfrom werkzeug.utils import secure_filenamefrom werkzeug.datastructures import FileStorageimport osdef create_app (): app = Flask(__name__) return app application=create_app() @application.route('/upload' , methods=['POST' , 'GET' ] ) def upload (): if request.method == 'POST' : f = request.files['file' ] basepath = os.path.dirname(__file__) upload_path = os.path.join(basepath, 'static\\uploads' ,secure_filename(f.filename)) f.save(upload_path) return redirect(url_for('upload' )) return render_template('upload.html' ) if __name__ == '__main__' : application.run()
upload.html:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" > <title > Title</title > </head > <body > <h1 > 文件上传示例</h1 > <form action ="" enctype ='multipart/form-data' method ='POST' > <input type ="file" name ="file" > <input type ="submit" value ="上传" > </form > </body > </html >
新建表的问题:链接