def user_regist(): form = RegistForm() if form.validate_on_submit(): # 查看用户名是否已经存在 user_name = form.user_name.data user_x = query_user_by_name(user_name) if user_x: flash("用户名已经存在!", category='err') return render_template('user_regist.html', form=form) # 如果用户不存在,执行注册 user = User() user.name = form.user_name.data user.pwd = form.user_pwd.data user.email = form.data['user_email'] user.age = form.user_edge.data user.birthday = form.data["user_birthday"] filestorage = request.files["user_face"] user.face = secure_filename_with_uuid(filestorage.filename) # 如果用户不存在,执行插入操作 insert_user_to_db(user) # 保存用户头像文件 try: photosSet.save(storage=filestorage, folder=user.name, name=user.face) 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 user_regist(): form = RegistForm() if form.validate_on_submit(): # 查看用户名是否已经存在 user_name = form.user_name.data user_x = query_user_by_name(user_name) if user_x: flash("用户名已经存在!", category='err') return render_template('user_regist.html', form=form) # 如果用户不存在,执行注册 user = User() user.name = form.user_name.data user.pwd = form.user_pwd.data user.email = form.data['user_email'] user.age = form.user_edge.data user.birthday = form.data["user_birthday"] filestorage = request.files["user_face"] user.face = secure_filename_with_uuid(filestorage.filename) # 如果用户不存在,执行插入操作 insert_user_to_db(user) # 保存用户头像文件 user_folder = os.path.join(app.config["UPLOADS_FOLDER"], user.name) create_folder(user_folder) filestorage.save(os.path.join(user_folder, user.face)) flash("用户注册成功!", category='ok') return redirect(url_for("user_login", username=user.name)) return render_template('user_regist.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 user_one = query_user_by_name(user_name) if user_one: # 返回注册界面,重新注册 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 = form.user_pwd.data # user.age = request.form["user_age"] user.age = form.user_age.data # user.birthday = request.form["user_birthday"] user.birthday = form.user_birthday.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 filerstorage = request.files["user_face"] # 获取头像文件 user.face = secure_filename_with_uuid( filerstorage.filename ) # secure_filename 文件名安全性检测,如果文件名有特殊字符,会将特殊字符转义,没有就返回原文件名 # print(user.face) # 如果不存在执行插入操作 # 插入一条数据 instert_user_to_db(user) # 保存用户头像文件 user_folder = os.path.join(app.config["UPLOADS_FOLDER"], user.name) create_folder(user_folder) # 创建用户文件夹 filerstorage.save(os.path.join(user_folder, user.face)) flash("注册成功!", category="ok") # username作为查询参数带到url中去 # 重定向页面 生成url 执行 user_login 函数 跳转到登录界面 return redirect(url_for("user_login", username=user.name)) return render_template("user_regist.html", form=form)