用pycharm社区版创建一个flask项目
安装flask模块
文件 -- 设置 --
创建一个空项目
在空项目里边new 2个 python package 取名为static和templates 删除它们的__init__.py文件
创建一个python文件 app.py
内容为
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello world!' if __name__ == '__main__': app.run()
在templates下创建一个html文件
login.html代码
在<title>登录</title>下面复制的是
Bootstrap5中文网 - Bootstrap框架 (bootstrapdoc.com)
网站首页往下滑
这两个
在<body>里面,复制了bootstrap网里边--快速入门--表单里边--复制的一个登录框代码
<body>
<div style="width:50%;margin:0 auto">
设置宽度 width:50%;div居中:"margin:0 auto"
</div>
<body>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
</head>
<body>
<form>
<div style="width:50%;margin:0 auto">
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
<div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1">
</div>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</body>
</html>
app.py增加了
from flask import Flask,render_template
render_template('login.html') #刚刚那个html文件名
render_template()函数是flask函数,它从模版文件夹templates中呈现给定的模板上下文
网站首页就会显示login.html页面
from flask import Flask,render_template
#使用Flask对象创建一个app对象
app = Flask(__name__)
#路由 / 访问的路径
@app.route('/')
def hello_world():
return render_template('login.html')
#登录
@app.route('/login')
def login():
return 'login html!'
#退出
if __name__ == '__main__':
app.run()
运行结果