flask day03

关于 git从本地上传到github

git init

将所有文件添加到仓库
git add

把所有文件提交到仓库,双引号是提交的注释内容
git commit -m "提交文件"

git remote add origin地址关联远程仓库(只需要关联一次)

//如果创建远程仓库的时候仓库中有默认文
git pull --rebase origin master

git push [-u]origin master - 提交(-u在第一次提交分之的时候才用)

蓝图 /blueprint

产生背景:管理路由地址

安装:pip install flask-blueprint

使用:

# 1.定义一个蓝图'first'
blue = Blueprint('first',__name__)
# 写路由'index'
@blue.route('/index/')
# 2.管理蓝图
app.register_blueprint(blueprint=blue)

# 3.跳转 

# ① 有参跳转: redirect即重定向
# redirect(url_for('生成蓝图的第一个参数.跳转的函数名称', 参数名=值, 参数名2=值2))
def stu():
   stu_scores = [90, 89, 92, 88]
   content = '

你真棒!

' return render_template('stu.html',scores = stu_scores,content_h2 = content ) # ② 无参跳转: # redirect(url_for('生成蓝图的第一个参数.跳转的函数名称')) return redirect(url_for('first.s_id')

注:下图代码在浏览器中访问/redirect_func/时会跳转至/s_id/1

@blue.route('/redirect_func/')
def redirect_func():
    # return redirect('/login/')
    # return redirect(url_for('first.login'))
    # return redirect('/s_id/1/')
    return redirect(url_for('first.s_id', id=1))

@blue.route('/s_id//')
def s_id(id):
    return 's_id %s' % id

装饰器 / decorator

①外层函数嵌套内层函数
②内层函数调用外层参数
③外层函数返回内层函数

def is_login(func):
    @wraps(func)
    # 加一个装饰器令使用装饰器的函数最后返回的函数是本身而不是装饰器
    def check():
        # 登陆过后就能访问index.html界面
        try:
            session['login_status']
            return func()
        except:
            # 没有登陆不让访问,跳转到session_login.html
            return render_template('session_login.html')
    return check

模板

Ⅰ.父模板
①定义网站模板的大体框架,定义可以被动态填充内容的block块
②block块定义: {% block name %} {% endblock %}
注意:block名称不能取同名
Ⅱ.子模板
①继承父模板: {% extends '父模板' %}
②动态可选择性的填充父模板中定义的block块, 使用{% block name %} 动态内容 {% endblock %}

解析标签:

{% 标签名 %} {% end标签名 %}
① for: {% for a in [] %} {% else %} {% endfor %}
② if: {% if 判断条件 %} {% else %} {% endif %}
③ block: {% block name %} {% endblock %}

解析变量:

{{ 变量名 }}
loop.index: 从1开始的值
loop.index0: 从0开始
loop.revindex: 倒序到1结束
loop.revindex0: 倒序到0结束
loop.first: 第一次循环返回True
loop.last: 最后一次循环返回True

注解: {# 注解内容 #}
过滤器

写法: {{ 变量 | 过滤器 }}
safe、lenth、upper

父模板 base.html




    
     
    
        {% block title %}
        {% endblock %}
    
    {% block css %}
    {% endblock %}
    {% block js %}
    {% endblock %}


    {% block content %}
    {% endblock %}


子模板

{% extends 'base_main.html' %}

{% block title %}
    学生列表页面
{% endblock %}
{% block css %}
    
    
    
    
{% endblock %}
{% block js %}
    
{% endblock %}

{% block content %}

{{ content_h2 | safe }}

{{ 'python' | length }}

{{ 'python' | length | upper }}

{{ 'python' | lower }}

{{ content_h2 | striptags }}

{{ scores }}

    {% for a in scores %} {{ loop.revindex0 }} {{ loop.first }} {{ loop.last }}
  • {{ a }}
  • {% else %}
  • 没有学生成绩数据
  • {% endfor %}
姓名 年龄
张三 28
李四 29
{% endblock %} 学生列表页面

你可能感兴趣的:(flask day03)