一对一模型 跟一对多写法是一样的,只是在添加反向引用的需要在里面加上uselist=False
content = db.relationship('Acontent', backref='article', uselist=False)
多对多模型
需要创建辅助表
tbl_tags = db.Table('tbl_tags',
db.Column('tag_id', db.Integer, db.ForeignKey('tbl_tag.id')), db.Column('article_id', db.Integer, db.ForeignKey('tbl_article.id')) )
自关联模型
自己关联自己
parent = db.relationship("Area", remote_side=[id]) # 自关联需要加remote_side
模板继承
语法
{% include(‘模板名称’) %}
{% include(‘目录/模板名称’) %}
导入头部header.html
{% include 'header.html' %}
主体内容
#导入底部footer.html
{% include 'footer.html' %}
#忽略模板文件不存在时的错误
{% include 'footer.html' ignore missing %}
#也可以组成模板列表,会按照顺序依次加载
{% include ['footer.html','bottom.html','end.html'] ignore missing %}
模板继承
父模板
{%block 名称%}
预留区域,可以编写默认内容,也可以没有默认内容
{%endblock %}
子模板
标签extends:继承,写在子模板文件的第一行。
{% extends "父模板路径"%}
{%block 名称%}
实际填充内容
{%endblock %}
可以通过super()来调用父模板内容
{% extends 'base.html' %}
{% block content %}
{{ super() }}
{% endblock %}
声明宏
{% macro 宏的名字(参数) %}
内容
{% endmacro %}
调用宏
{{ 宏的名字(参数) }}
无参宏
{#声明#}
{% macro macro_input() %}
输入框:<input type="text"> <br data-tomark-pass>
{% endmacro %}
{#调用#}
{{ macro_input() }}
{{ macro_input() }}
有参宏
{#声明#}
{% macro macro_input(tip,type,name) %}
{{ tip }}<input type="{{ type }}" name="{{ name }}"> <br data-tomark-pass>
{% endmacro %}
{#调用#}
{{ macro_input('账号:','text','email') }}
{{ macro_input('密码:','password','pwd') }}
缺省宏
{#声明#}
{% macro macro_input(tip,type,name,value='123') %}
{{ tip }}<input type="{{ type }}" name="{{ name }}" value="{{ value }}"> <br data-tomark-pass>
{% endmacro %}
{#调用#}
{{ macro_input('账号:','text','email') }}
{{ macro_input('密码:','password','pwd','123456789') }}
导入宏
{#声明#}
{% macro macro_input(tip,type,name,value='123') %}
{{ tip }}<input type="{{ type }}" name="{{ name }}" value="{{ value }}"> <br data-tomark-pass>
{% endmacro %}
注意虽然是html文件,但是没有html文件的结构。
{% from 'common_macro.html' import macro_input %}
{#别名#}
{#{% from 'common_macro.html' import macro_input as input %}#}
{#调用#}
{{ macro_input('账号:','text','email') }}
{{ macro_input('密码:','password','pwd','123456789') }}
宏的内部变量
varargs : 这是一个列表。如果调用宏时传入的参数多于宏声明时的参数,多出来的没指定参数名的参数就会保存在这个列表中。
kwargs : 这是一个字典。如果调用宏时传入的参数多于宏声明时的参数,多出来的指定了参数名的参数就会保存在这个字典中。
{#声明#}
{% macro macro_input(tip,type,name,value='123') %}
{{ tip }}<input type="{{ type }}" name="{{ name }}" value="{{ value }}"> <br>
<br data-tomark-pass>{{ varargs }}
<br data-tomark-pass> {{ kwargs }}
<br data-tomark-pass> {% endmacro %}
{{ macro_input('账号:','text','email',11,22,33,44,age=12) }}
{{ macro_input('密码:','password','pwd','123456789') }}