flask博客系统在设计管理员帐号的时候是这样考虑的:起初系统部署是数据库中是不存在管理员信息的,这时候帐号登录直接跳转到管理员注册界面,注册完管理员信息后可以直接用注册帐号进行登录系统进行管理系统

登录路由代码

@admin.route('/login', methods=['GET', 'POST'])
def login():
    login_form = LoginForm(prefix='login')
    user = User.query.filter_by(status=True).first()
    if not user:
        add_admin_form = AddAdminForm(prefix='add_admin')
        if add_admin_form.validate_on_submit():
            u = User(username=add_admin_form.username.data.strip(),
                     email=add_admin_form.email.data.strip(),
                     password=add_admin_form.password.data.strip(),
                     status=True, role=True
                     )
            db.session.add(u)
            db.session.commit()
            login_user(user=u)
            return redirect(url_for('admin.index'))

        return render_template('admin/add_admin.html', addAdminForm=add_admin_form)
    else:
        if login_form.validate_on_submit():
            u = User.query.filter_by(username=login_form.username.data.strip()).first()
            if u is None:
                flash({'error': '帐号未注册!'})
            elif u is not None and u.verify_password(login_form.password.data.strip()) and u.status:
                login_user(user=u, remember=login_form.remember_me.data)
                return redirect(url_for('admin.index'))
            elif not u.status:
                flash({'error': '用户已被管理员注销!'})
            elif not u.verify_password(login_form.password.data.strip()):
                flash({'error': '密码不正确!'})

    return render_template('admin/login.html', loginForm=login_form)

登录form 代码

class LoginForm(FlaskForm):
    username = StringField('帐号', validators=[DataRequired()])
    password = PasswordField('密码', validators=[DataRequired()])
    remember_me = BooleanField(label='记住我', default=False)
    submit = SubmitField('登 录')


class AddAdminForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired(), Length(1, 16, message='用户名长度要在1和16之间')])
    email = StringField('邮箱', validators=[DataRequired(), Length(6, 64, message='邮件长度要在6和64之间'),
                        Email(message='邮件格式不正确!')])
    password = PasswordField('密码', validators=[DataRequired(), EqualTo('password2', message='密码必须一致!')])
    password2 = PasswordField('重输密码', validators=[DataRequired()])
    submit = SubmitField('注 册')

    def validate_username(self, field):
        if User.query.filter_by(username=field.data).first():
            raise ValidationError('用户名已被注册!')

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('邮箱已被注册!')

登录注册模板代码

登录模板

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
    <meta name="generator" content="Jekyll v3.8.6">
    <link rel="icon" href="{{ url_for('admin.static', filename='images/favicon.ico')}}">
    <title>博客登陆</title>

    <!-- Bootstrap 4.4.1 core CSS -->
    <link href="{{ url_for('admin.static', filename='css/bootstrap.min.css')}}" rel="stylesheet">
    <!-- Favicons -->
    <link rel="icon" href="{{ url_for('admin.static', filename='img/favicon.ico')}}">
    <meta name="theme-color" content="#563d7c">


    <style>
        .bd-placeholder-img {
            font-size: 1.125rem;
            text-anchor: middle;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
        }

        @media (min-width: 768px) {
            .bd-placeholder-img-lg {
                font-size: 3.5rem;
            }
        }

        body {
            background-image:url(https://www.h3blog.com/bing_bg);
            background-size: cover; 
        }
        #login {
            filter:alpha(Opacity=80);
            -moz-opacity:0.8;
            opacity: 0.8;
        }
    </style>
    <!-- Custom styles for this template -->
    <link href="{{ url_for('admin.static', filename='css/login.css')}}" rel="stylesheet">
    <link href="{{ url_for('admin.static',filename='css/toastr.min.css')}}" rel="stylesheet">
</head>

<body>
    <div class="container">
        <div class="row justify-content-center">
            <span class="mb-4 font-weight-bold" style="font-size: xx-large;">H3BLOG</span>
        </div>
        <div class="row justify-content-center">
            <div id="login" class="card">
                <div class="card-body">
                    <form class="form-signin" method="post">
                        {{ loginForm.hidden_tag() }}

                        <!-- <h1 class="h3 mb-3 font-weight-normal">博客登陆</h1> -->
                        <label for="username" class="sr-only">帐号</label>
                        {{ loginForm.username(id="username",class="form-control", placeholder="帐 号",required="", autofocus="") }}
                        <label for="password" class="sr-only">密码</label>
                        {{ loginForm.password(id="password",class="form-control",placeholder="密 码",required="")}}
                        <div class="checkbox mb-3">
                            <label>
                                {{ loginForm.remember_me() }} {{ loginForm.remember_me.label(class="small") }}
                            </label>
                        </div>
                        {{ loginForm.submit(class="btn btn-ls btn-primary btn-block") }}
                    </form>
                </div>
            </div>
            <!--end card-->
        </div>
    </div>
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="{{ url_for('admin.static',filename='js/jquery.min.js') }}"></script>
    <script src="{{ url_for('admin.static',filename='js/popper.min.js') }}"></script>
    <script src="{{ url_for('admin.static',filename='js/bootstrap.min.js') }}"></script>
    <script src="{{ url_for('admin.static',filename='js/toastr.min.js')}}"></script>
    <script src="{{ url_for('admin.static',filename='js/admin.js')}}"></script>
    {% include 'admin/common/alert.html' %}
</body>

</html>

注册模板代码

<!DOCTYPE html>
<html lang="zh">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <meta name="description" content="">
        <meta name="author" content="">
        <link rel="icon" href="{{ url_for('admin.static', filename='images/favicon.ico')}}">

        <title>{% block title %}何三笔记 {% endblock %}</title>
        {% block css %}
            <!-- 新 Bootstrap 核心 CSS 文件 -->
            <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">

            <!-- 可选的Bootstrap主题文件(一般不用引入) -->
            <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">

            <!-- 个性化主题文件 -->

            <link href="{{ url_for('admin.static', filename='css/admin.css')}}" rel="stylesheet">
        {% endblock %}

        {% block js %}
            <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
            <script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>

            <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
            <script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

            <!-- 个性化 js 文件 -->
            <script src="{{ url_for('admin.static', filename='js/admin.js')}}"></script>
        {% endblock %}
    </head>

    <body>
        <div class="container">
            <div class="row">
                <div class="add-admin">
                    <div class="row">
                        <div class="col-xs-12 col-sm-offset-4">
                            <div class="col-xs-12 col-sm-4">
                                <form method="post" role="form">
                                    {{ addAdminForm.hidden_tag() }}
                                    <div class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i> </span>
                                        {{ addAdminForm.username(class="form-control", placeholder="输入管理员用户名", required="", autofocus="") }}
                                    </div>
                                    <div class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i> </span>
                                        {{ addAdminForm.email(class="form-control", placeholder="输入管理员邮箱", required="") }}
                                    </div>
                                    <div class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i> </span>
                                        {{ addAdminForm.password(class="form-control", placeholder="输入管理员密码", required="") }}
                                    </div>
                                    <div class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i> </span>
                                        {{ addAdminForm.password2(class="form-control", placeholder="再次输入管理员密码", required="") }}
                                    </div>
                                    <div class="pull-right">
                                        <input class="btn btn-default" type="reset" value="重 置" style="margin-right: 10px">
                                        {{ addAdminForm.submit(class="btn btn-primary") }}
                                    </div>
                                </form>
                                <div class="col-xs-12">
                                    <br>
                                    <div class="row">
                                    {% for field_name, field_errors in addAdminForm.errors|dictsort if field_errors %}
                                        {% for error in field_errors %}
                                            <div class="alert alert-danger alert-dismissible" role="alert">
                                                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                <strong>{{ addAdminForm[field_name].label }}错误:</strong> {{ error }}
                                            </div>
                                        {% endfor %}
                                    {% endfor %}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div><!--/row-->
        </div><!--/.container-->
    </body>
</html>