赞
踩
flask版本:1.1.4
jinjia2版本:2.11.3
flask-script版本:2.0.6
flask-blueprint版本:1.3.0
flask-bootstrap版本:3.3.7.1
flask英文文档:https://flask.palletsprojects.com/en/1.1.x/
flask中文文档:https://dormousehole.readthedocs.io/en/1.1.2/
jinja2英文文档:https://jinja.palletsprojects.com/en/3.0.x/
flask-bootstrap中文文档:https://flask-bootstrap-zh.readthedocs.io/zh/latest/
bootstrap3组件文档:https://v3.bootcss.com/components/
安装最新:pip install flask-script
安装指定版本:pip install flask-script==2.0.6
from flask import Flask
from flask_script import Manager
app = Flask(__name__)
manager = Manager(app=app)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
# app.run()
manager.run()
python app.py runserver -r -d
默认127.0.0.1:5000访问,可以指定端口号,及外网ip来进行访问:
python app.py runserver -r -d --port 80 --host 0.0.0.0
pip install flask-blueprint
from flask import Blueprint
blue = Blueprint('blue', __name__)
@blue.route('/')
def hello_world():
return 'Hello World!'
@blue.route('/index/')
def index():
return 'Hello Index!'
from flask import Flask
from flask_script import Manager
from route import blue
app = Flask(__name__)
app.register_blueprint(blueprint=blue)
manager = Manager(app=app)
if __name__ == '__main__':
# app.run()
manager.run()
http://127.0.0.1:5000
跳转至:
http://127.0.0.1:5000/home/
从home跳转到 hello_world
from flask import Blueprint from flask import render_template from flask import Response, request, redirect, url_for blue = Blueprint('blue', __name__) @blue.route('/') def hello_world(): return 'index' @blue.route('/setCookie/') def setCookie(): rsp = Response('set cookie, 21') rsp.set_cookie('myCookie', '21') return rsp @blue.route('/getCookie/') def getCookie(): myCookie = request.cookies.get('myCookie') return 'cookie is {}'.format(myCookie) @blue.route('/deleteCookie/') def deleteCookie(): rsp = redirect(url_for('blue.hello_world')) rsp.delete_cookie('myCookie') return rsp
app.py
route.py
login.html
route.py
{% %} 控制语句,如
{% if data == 'name' %}
{{ data }}
{% else %}
{{ data }}
{% endif %}
{{ }} 变量,如 {{ data }}
{# #} 注释,如 {# data is xxxx! #}
base.html
home.html
定义一个宏 macro
引入宏
如获取字典的长度
{% set number = data['groupNum'] | length %}
pip install flask-bootstrap
{% extends "bootstrap/base.html" %}
{% block styles %}
{{ super() }}
<link rel="stylesheet" href="{{ url_for('static', filename='home.css') }}">
{% endblock %}
{% block content %}
<h2>hello</h2>
<h3>flask</h3>
{% endblock %}
$("#test_id").click(function(){
alert('test!test!')
});
{% extends "bootstrap/base.html" %}
{% block scripts %}
{{super()}}
<script src="{{url_for('static', filename='home.js')}}"></script>
{% endblock %}
{% block content %}
<button id="test_id">click</button>
{% endblock %}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。