def user_regist(): form = RegistForm() if form.validate_on_submit(): # 查看用户名是否已经存在 user_name = form.user_name.data user_x = User.query.filter_by(name=user_name).first() if user_x: flash("用户名已经存在!", category='err') return render_template('user_regist.html', form=form) # 如果用户不存在,创建一个用户类的实例 user = User() user.name = form.user_name.data user.pwd = generate_password_hash(form.user_pwd.data) user.email = form.data['user_email'] user.phone = form.user_phone.data user.jianjie = form.user_jianjie.data user.uuid = str(uuid.uuid4().hex)[0:10] # 给每个用户分配一个10个字符的身份标识符 filestorage = request.files["user_face"] user.face = secure_filename_with_uuid(filestorage.filename) # 保存用户头像文件,执行插入操作 try: photosSet.save(storage=filestorage, folder=user.name, name=user.face) db.session.add(user) db.session.commit() flash("用户注册成功!", category='ok') return redirect(url_for("user_login", username=user.name)) except UploadNotAllowed: flash("头像文件格式不对!", category='err') return render_template('user_regist.html', form=form) return render_template('user_regist.html', form=form)
def register(): form = RegisterForm() if form.validate_on_submit(): # 检查用户上传的图像文件是否符合要求 if not check_files_extension([form.photo.data.filename], ALLOWED_IMAGE_EXTENSION): flash('图片格式不正确!', 'danger') return redirect(url_for('auth.register'), form=form) user_name_db = User.query.filter( User.name == form.user_name.data).first() if user_name_db: flash('用户名已经存在!', 'danger') return render_template('register.html', form=form) user_email_db = User.query.filter( User.email == form.email.data).first() if user_email_db: flash('邮箱已经被注册过!', 'danger') return render_template('register.html', form=form) user_phone_db = User.query.filter( User.phone == form.phone.data).first() if user_phone_db: flash('手机号已经被注册过!', 'danger') return render_template('register.html', form=form) user = User() user.name = form.user_name.data user.pwd = generate_password_hash(form.user_pwd.data) user.email = form.email.data user.phone = form.phone.data user.introduce = form.introduce.data user.birthday = form.birthday.data # 文件上传 # img_file = request.files['photo'] img_file = request.files.get('photo') user.photo = secure_filename(img_file.filename) flash('注册成功', 'success') db.session.add(user) db.session.commit() # file_path = file_bastpath + img_file.filename user_folder = os.path.join(app.config['UPLOADED_FOLDER'], user.name) create_folder(user_folder) img_file.save(os.path.join(user_folder, user.photo)) return redirect(url_for('auth.login', user_name=user.name)) # user_name = form.data['user_name'] return render_template('register.html', form=form)
def user_regist(): # 注册 form = RegistForm() if form.validate_on_submit( ): # 检查提交方式是否为post 验证forms.py定义的validators 验证是否通过 # 检查用户上传的头像文件名是否符合要求 # if not check_files_extension([form.user_face.data.filename], ALLOWED_IMAGEEXTENSIONS): # flash("头像文件格式错误!", category="err") # return render_template("user_regist.html", form=form) # 查看用户是否存在 user_name = form.user_name.data query_user_by_name = User.query.filter_by(name=user_name).first() if query_user_by_name: # 返回注册界面,重新注册 flash("用户名已存在!", category="err" ) # Flashes a message to the next request 闪现一条消息到下一次消息请求 return render_template("user_regist.html", form=form) query_user_by_email = User.query.filter_by( email=form.user_email.data).first() if query_user_by_email: # 返回注册界面,重新注册 flash("用户邮箱已被注册注册!", category="err" ) # Flashes a message to the next request 闪现一条消息到下一次消息请求 return render_template("user_regist.html", form=form) query_user_by_phone = User.query.filter_by( phone=form.user_phone.data).first() if query_user_by_phone: # 返回注册界面,重新注册 flash("手机号已被注册!", category="err" ) # Flashes a message to the next request 闪现一条消息到下一次消息请求 return render_template("user_regist.html", form=form) # print("form", form.user_name.data) # print("form", form.data) # print("form", form.data["user_name"]) # print("request.form", request.form) user = User() # user.name = request.form["user_name"] user.name = form.user_name.data # user.pwd = request.form["user_pwd"] user.pwd = generate_password_hash(form.user_pwd.data) # user.age = request.form["user_age"] user.phone = form.user_phone.data # user.birthday = request.form["user_birthday"] user.jianjie = form.user_jianjie.data # user.email = request.form["user_email"] user.email = form.user_email.data # user.face = request.form["user_face"] # user.face = form.user_face.data # filerstorage = form.user_face.data user.uuid = str(uuid.uuid4().hex)[0:10] # 10个字符长度 filerstorage = request.files["user_face"] # 获取头像文件 user.face = secure_filename_with_uuid( filerstorage.filename ) # secure_filename 文件名安全性检测,如果文件名有特殊字符,会将特殊字符转义,没有就返回原文件名 # print(user.face) # 保存用户头像文件 # user_folder = os.path.join(app.config["UPLOADS_FOLDER"], user.name) # create_folder(user_folder) # 创建用户文件夹 # filerstorage.save(os.path.join(user_folder, user.face)) try: photosSet.save(storage=filerstorage, folder=user.name, name=user.face) # 如果不存在执行插入操作,创建一个用户类 User 的实例 # 插入一条数据 db.session.add(user) db.session.commit() flash("注册成功!", category="ok") # username作为查询参数带到url中去 # 重定向页面 生成url 执行 user_login 函数 跳转到登录界面 return redirect(url_for("user_login", username=user.name)) except UploadNotAllowed: flash("头像文件格式错误!", category="err") return render_template("user_regist.html", form=form) return render_template("user_regist.html", form=form)