开始Flask项目

  1. 新建Flask项目。
  2. 设置调试模式。
  3. 理解Flask项目主程序。
  4. 使用装饰器,设置路径与函数之间的关系。
  5. 使用Flask中render_template,用不同的路径,返回首页、登录员、注册页。
  6. 用视图函数反转得到URL,url_for(‘login’),完成导航里的链接。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}{% endblock %}导航</title>
    <link rel="stylesheet" type="text/css" href="../static/css/daohang.css">
    <script src="../static/js/daohang.js"></script>
    <link rel="stylesheet" href="{{ url_for('static',filename='css/daohang.css') }}">
{% block head %}{% endblock %}
</head>
<body id="myBody">
<nav class="navbar">
    <a href="{{ url_for('daohang') }}">首页</a>
    <a href="{{ url_for('denglu') }}">登录</a>
    <a href="{{ url_for('zhuce') }}">注册</a>
    <input type="text" name="search" placeholder="请输入关键字">
    <button type="submit">查找</button>
    <select class="navigate">
        <option>个人中心</option>
        <option>收藏</option>
        <option>点击</option>
    </select>
    <img id="myOnOff" src="http://www.runoob.com/images/pic_bulbon.gif" onclick="mySwitch()" width="20px">
</nav>
{% block main %} {% endblock %}
</body>
from flask import Flask, render_template

app = Flask(__name__)


@app.route('/')
def daohang():
    return render_template('daohang.html')


@app.route('/denglu/')
def denglu():
    return render_template('denglu.html')


@app.route('/zhuce/')
def zhuce():
    return render_template('zhuce.html')


if __name__ == '__main__':
    app.run()
原文地址:https://www.cnblogs.com/xiaojiaqi/p/7780594.html