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)
def add_user(): if request.method == 'GET': return render_template('back/add_user.html') if request.method == 'POST': username = request.form.get('username') password = request.form.get('password') tel = request.form.get('tel') email = request.form.get('email') if username and password and tel and email: user = User.query.filter(User.username == username).first() if user: error = '姓名已存在' return redirect(url_for('back.add_user', error=error)) else: user = User() user.username = username user.password = password user.tel = tel user.email = email db.session.add(user) db.session.commit() return redirect(url_for('back.user_list')) else: error = '请填写完整用户信息' return render_template('back/add_user.html', error=error)