1.新建Flask项目。
2.设置调试模式。
3.理解Flask项目主程序。
4.使用装饰器,设置路径与函数之间的关系。
5.使用Flask中render_template,用不同的路径,返回首页、登录员、注册页。
6.用视图函数反转得到URL,{{url_for(‘login’)}},完成导航条里的链接。
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template("index.html")
@app.route('/login/')
def login():
return render_template("login.html")
@app.route("/register/")
def register():
return render_template("register.html")
if __name__ == '__main__':
app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>欢迎登陆</title>
<base href="http:www.gzcc.cn/"target="_blank">
<script>
function mySwitch(){
var uBody=document.getElementById("myBody");
var Onoff=document.getElementById("myOnOff");
if(Onoff.src.match("bulbon")){
Onoff.src="https://www.runoob.com/images/pic_bulboff.gif";
uBody.style.background="black";
uBody.style.color="white";
}else{
Onoff.src="https://www.runoob.com/images/pic_bulbon.gif";
uBody.style.background="white";
uBody.style.color="black";
}
}
</script>
</head>
<body id="myBody">
<nav>
<img src="http://gifxiu8.cqyzwxm.com:8088/upload/20150910/204849_1093.jpg" height="100px" width="450px"><br>
<a href="{{ url_for("index") }}">首页</a>
<a href="">新闻</a>
<input type="text"name="search">
<button type="submit">搜索</button>
<a href="{{ url_for("login") }}">登陆</a>
<a href="{{ url_for("register") }}">注册</a>
<a href="">退出</a>
<img id="myOnOff" οnclick="mySwitch()" src="https://www.runoob.com/images/pic_bulbon.gif"height="20px" width="30px">
</nav>