使用Flask-Moment本地化日期和时间

在这里需要导入Flask-moment拓展

初始化flask-moment

from flask_moment import Moment
moment = Moment(app)

在templates/base.html中引入Moment.js库

{% block scripts %}
{{super()}}
{{moment.include_moment()}}
{% endblock %}

添加一个datetime变量
在.py文件中

form datetime import datetime

@app.route('/')
def index():
    return render_template('index.html',current_time=datetime.utcnow())

在狗书第32页中只给出了

<p>The local date and time is {{ moment(current_time).format('LLL') }}.</p>
<p>That was {{ moment(current_time).fromNow(refresh=True) }}.</p>

代码,在Chrome中测试时间戳载入失败,在查看其它博客我发现了解决方法
需要在开头加上

<head>
<title>Flask-Moment example app</title>
    {{  moment.include_jquery()}}
    {{  moment.include_moment()}}
</head>

具体原因不明

web表单

配置,flask-wtf无需再应用层初始haul,但是它要求应用配置一个秘钥

app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'

app.config字典可用于存储flask,拓展和应用自身的配置变量
(防止恶意攻击)

from flask_wtf import FlaskForm   #导入wtf拓展表单类
from wtforms import StringField,SubmitField   #导入表单需要的字段,StringField 表示属性type="text"的
                                                html<input>元素
from wtforms.validatoes import DataRequired   #导入wtf拓展提供的表单验证器

class NameForm(FlaskForm):                #继承于flaskform
    name = StringField('What is your name?',validators=[DataRequired()])
    submit = SubmitField('Submit')   #SubmitField表示属性为type = "submit"的html<input>元素

WTForms支持的HTML标准字段以及验证函数在狗书36页有所提及

以下是学习代码

from flask import Flask,render_template,session,redirect,url_for,flash
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import StringField,SubmitField
from wtforms.validators import DataRequired

app = Flask(__name__)
app.config['SECRET_KEY'] = 'It is a key'
bootstrap = Bootstrap(app)

class NameForm(FlaskForm):
    name = StringField('请输入你的名字',validators = [DataRequired()])
    submit = SubmitField('提交')

@app.route('/', methods=['GET', 'POST'])
def index():
    #实例话NameForm类,此时每次填写即表示单独一个实例
    form = NameForm()
    #判断是否提交,如果提交了,则将提交的数据赋值到name变量,传给模版
    if form.validate_on_submit():
        #session是字典结构,get方法获取不到的话则返回None,这里第一次提交明显获取不到,所以返回None
        old_name = session.get('name')
        #判断old_name非空且不等于表单的输入数据时执行
        if old_name is not None and old_name != form.name.data:
            # flash可以把内容显示出来
            flash('Looks like you have changed your name!')
        #第一次提交时,session还无值,所以在这边给session赋值,赋值方法就是字典添加数据方法。
        #form.name.data表示:form是NameForm实例,即NameForm下的name字段的data数据,即表单输入数据
        session['name'] = form.name.data
        #依据书本内容重定向到首页
        return redirect(url_for('index'))
    #渲染模版,同时传回数据给模版,name=name第一个name表示变量name,即前端可以通过name变量获得值,
    #后一个name则表示数据,再这里就是form.name.data
    return render_template('index.html', form=form, name=session.get('name'))

@app.errorhandler(404)
def notfond(e):
    return render_template('404.html'),404

HTML
base.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% extends "bootstrap/base.html" %}

{% block title %}Flasky{% endblock %}

{% block navbar %}
<div class="navbar navbar-inverse" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="/">Flasky</a>
    </div>
    <div class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li><a href="/">Home</a></li>
      </ul>
    </div>
  </div>
</div>
{% endblock %}

{% block content %}
<div class="container">
    {% block page_content %}{% endblock %}
</div>
{% endblock %}
{% block scrips %}
{{ super() }}
{{ moment.include_moment() }}
{% endblock %}

</body>
</html>

index.html(使用flask-wtf和flask-bootstrap渲染表单)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>TITLE</title>
</head>
<body>
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block page_content %}
<div class="page-header"><h1>Hello,{% if name %}{{ name }}{% else %}Strang{% endif %}!</h1>
</div>
{{ wtf.quick_form(form) }}
{% endblock %}
</body>
</html>

404.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>页面不见咯</title>
</head>
<body background="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1552730708679&di=33a34ab144c3f47ecd0da1ab5118edcd&imgtype=0&src=http%3A%2F%2Fimgs.aixifan.com%2Flive%2F1484794670285%2F1484794670285.png">
<center>404NOT FOUND</center>
</body>
</html>

html中重点

{% import "bootstrap/wtf.html" as wtf %}
{{ wtf.quick_form(form) }}

接下来的就是到我学习的地方了。
sql数据库