昨天 上午把Flask连接sqlite的数据库问题解决后, 昨天下午学习了Flask-wtf, 巧的是前天晚上睡前在床看看书时,看到了wtforms介绍, 觉得很有意思,因为没使用过,所以无法大脑里想像出渲染后的样式.昨天下午正好有教学视频讲解到了使用flask-wtf于是跟着视频做了一个简单的登陆验证.
下面是app.py , app.config.from_object(Config)一定要在前面, 写db后面会报错,奇怪的是报错也能正常运行.写前面就不报错了.
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from config import Config
import click
from forms import RegisterForm
app = Flask(__name__)
bootstrap = Bootstrap(app)
app.config.from_object(Config) #一定要在SQLALCHEMY实例化对象前面
db = SQLAlchemy(app)
@app.route('/')
def index():
title = 'This is in app.py give to base'
return render_template('index.html', title=title)
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm()
if form.validate_on_submit():
pass
return render_template('register.html', form=form)
@app.cli.command()
def hello():
click.echo('This is click command')
下面是config.py, 上面的代码写 from config import Config,引入 的就是这个config.py里面的Config类
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
#SECRET_KEY
SECRET_KEY = os.environ.get('SECRET_KEY')# or 'asdf865zx3' or前面的是需要在windows系统环境添加一条SECRET_KEY或者直接写明文密码
#CONNECT_SQLITE
# SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir,'app.db')
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(os.environ.get('DATABASE_URL'), 'app.db')
# SQLALCHEMY_DATABASE_URI = 'sqlite:///D:\\db\\app.db' #os.environ.get("DATABASE_URL")
SQLALCHEMY_TRACK_MODIFICATIONS = False
下面是forms.py
from flask_wtf import FlaskForm
#下面RegisterForm继承的就是上面import 引入的是FlaskForm类
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired, Length, EqualTo, Email
#但是验证的任务交给 wtforms,要提一下的是,Email验证报错的话,是pip install wtforms版本太高,
#解决方法有两个
# 第一个方法 :安装低版本,百度一下哪一个合适.
# 第二个方法 : 单独 pip install email-validator, 也就是另安装 email-validator
class RegisterForm(FlaskForm):
#验证网页端post提交过来的Username, Password, Reapeat Password, Email, 也就是用户名,密码,二次密码,邮箱,最后还有验证提交注册
username = StringField('Username', validators=[DataRequired(), Length(min=6, max=20)])
password = PasswordField('Password', validators=[DataRequired(), Length(min=6, max=30)])
confirm = PasswordField('Repeat Password', validators=[DataRequired(), EqualTo('password')])
email = StringField('Email', validators=[DataRequired(), Email()])
submit = SubmitField('注册')
下面是Index.html, 继承了自己编的base.html
{% extends "base.html" %}
{% block app_content %}
<h1> This is in index content </h1>
{% endblock %}
下面是自己编的base.html, 它又继承了flask-bootstrap的base.html, 而且引用了navbar.html
{% extends 'bootstrap/base.html' %}
{% block title %}
{% if title %}
{{ title }} | Flask App #感觉不是太理解这个 | , 好像是正则里面的 or
{% else %}
Flask App
{% endif %}
{% endblock %}
{% block navbar %}
{% include 'navbar.html' %}
{% endblock %}
{% block content %}
<div class="container">
{% block app_content %}
{% endblock %}
</div>
{% endblock content %}
下面是navbar.html, 这个html是直接复制 组件 · Bootstrap v3 中文文档 | Bootstrap 中文网 (bootcss.com)
导航案例, 将导航条上面只保留了三个按钮.左边两个, 右边一个
<nav class="navbar navbar-default">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<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="#">Flask</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="{% if request.endpoint == 'index' %}active{% endif %}">
<a href="{{ url_for('index') }}">Home </a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="{% if request.endpoint == 'register' %}active{% endif %}">
<a href="{{ url_for('register') }}">注册</a>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
下面是Register.html, 它也继承了base.html,保留了base的导航条, 只是重写了app_content, 其实就是注册时那四个要填写的username, password, repeat password, email
{% extends 'base.html' %}
{% block app_content %}
<h1>Register Now</h1>
<div class="row">
<div class="col-md-6">
{% import 'bootstrap/wtf.html' as wtf%}
{{ wtf.quick_form(form) }}
</div>
</div>
{% endblock %}
wtf强大的之处就是体现在
{% import 'bootstrap/wtf.html' as wtf%} 将bootstrap/wtf.html元素都import引入进来,引进来的有函数,变量. as wtf 的意思 是简写wtf,方便后面使用
{{ wtf.quick_form(form) }} 这一行就是wtf 的精华, 一条 wtf.quick_form(from), 就将注册时那四个要填写的username, password, repeat password, email
给渲染出来了.不用单独写html代码
太棒了.
下面是网页效果
下面是提交注册后的效果