项目结构
├── flask_demo.py # 项目主文件 ├── secret_key.py ├── static # 静态文件目录 │ └── main.css └── templates # 模板文件目录 ├── hello.html ├── login.html └── upload.html
代码文件
flask_demo.py
from flask import Flask from flask import session, redirect, abort, url_for from flask import request, render_template, make_response app = Flask(__name__) # 主页,登陆后显示登录名 @app.route('/') def index(): username = session.get("username") app.logger.info("index") # 日志 return "Hello world %s"%username # 路由参数变量 @app.route("/user/<username>") def show_username(username): return "username:%s"%username # 参数转换器 @app.route("/id/<int:uid>") def show_id(uid): return "your id:%d"%uid # post, get请求 @app.route("/username", methods=["POST", "GET"]) def username(): if request.method == "POST": return "post username" elif request.method == "GET": return "get username" else: return "none" # 模板和静态文件 @app.route("/hello/") @app.route("/hello/<name>") def hello(name=None): return render_template("hello.html", name=name) # 表单post参数 @app.route("/login/", methods=["GET", "POST"]) def login(): if request.method == "GET": return render_template("login.html") elif request.method == "POST": username = request.form.get("username") password = request.form.get("password") # session会话 session["username"] = username return redirect(url_for("index")) # return "username:%s, password:%s"%(username, password) # 退出登录 @app.route("/logout/") def logout(): session.pop("username", None) return redirect(url_for("index")) # 推荐 get参数:http://127.0.0.1:5000/page/?num=4 @app.route("/page/") def page(): page_num = request.args.get("num") return "num:%s"%page_num # 文件上传 @app.route("/upload/", methods=["GET", "POST"]) def upload_file(): if request.method == "GET": return render_template("upload.html") elif request.method == "POST": file = request.files.get("upload-file") print(file.filename) file.save(file.filename) return "文件上传成功" # 读取存贮 cookies @app.route("/cookie/") def cookie(): username = request.cookies.get("username") print(username) response = make_response("访问cookie") if not username: response.set_cookie("username", "mouday") return response # 404 与重定向 @app.route("/notpage1/") def notpage1(): return redirect(url_for("notpage2")) @app.route("/notpage2/") def notpage2(): abort(401) # 404页面 @app.errorhandler(404) def not_find_page(error): return "<h1>404</h1>", 404 app.secret_key = r"\xedz\xdf\x1c\xd2\xa7\xdff:6\xef\xf2\x95mL\x18TF\r\x00LM\xec\xa9" if __name__ == "__main__": # 监听所有公网 IP, 开启调试模式 app.run(host='0.0.0.0', debug=True)
secret_key.py
# 可用于生成 secret_key import os print(os.urandom(24)) # b'\xedz\xdf\x1c\xd2\xa7\xdff:6\xef\xf2\x95mL\x18TF\r\x00LM\xec\xa9'
main.css
body{ background-color: darkgreen; }
hello.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link href="https://www.ctyun.cn/portal/link.html?target=%2Fstatic%2Fmain.css" rel="stylesheet" type="text/css" /> </head> <body> <h1>hello world</h1> {% if name %} <h2>{{ name }}</h2> {% else %} <h2>没有名字</h2> {% endif %} </body> </html>
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/login/" method="post"> <input type="text" name="username"> <input type="text" name="password"> <input type="submit"> </form> </body> </html>
upload.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/upload/" method="post" enctype="multipart/form-data"> <input type="file" name="upload-file"> <input type="submit"> </form> </body> </html>