Exemplo n.º 1
0
def register():
    if request.method == 'GET':
        return render_template('back/register.html')
    if request.method == 'POST':
        #获取数据
        username = request.form.get('username')
        password = request.form.get('password')
        password1 = request.form.get('password1')
        number = request.form.get('number')
        email = request.form.get('email')
        # 判断用户名是否注册过,如果注册在判断密码
        user = User.query.filter(User.name == username).first()
        if user:
            #用户存在
            error = '用户名已存在'
            return render_template('back/register.html', error=error)
        else:
            if password1 == password:
                #保存数据
                user = User()
                user.name = username
                # user.password = password
                #给密码编码
                user.password = generate_password_hash(password)
                user.email = email
                user.number = number
                user.save()
                return redirect(url_for('back.home'))
            else:
                #密码不一样
                error = '两次输入的密码不一致'
                return render_template('back/register.html', error=error)
Exemplo n.º 2
0
def register():
    if request.method == 'GET':
        return render_template('back/register.html')
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('userpwd')
        password2 = request.form.get('userpwd2')
        if username and password and password2:  #判断输入数据是否有为空
            user_res = User.query.filter(User.name == username).first()
            if user_res:  # 判断是否注册
                error = '该用户名已注册,请更换!'
                return render_template('back/register.html',
                                       err=error)  #error需要页面的{{error}}接收
            if password != password2:
                error = '重复密码不一致!'
                return render_template('back/register.html', err=error)
            #保存用户
            user = User()
            user.name = username
            user.pwd = generate_password_hash(password)  #加密保存到数据库
            db.session.add(user)
            db.session.commit()
            return redirect(url_for('first.login'))
        return render_template('back/register.html')