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(): # 查看用户名是否已经存在 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 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( ): # 检查提交方式是否为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)
def user_regist(): form = RegistForm() if form.validate_on_submit(): user_name = request.form['user_name'] #判断用户是否存在 user_x = User.query.filter_by(name=user_name).first() if user_x: flash('用户已经存在', category='err') else: user = User() user.name = user_name user.password = request.form['user_password'] user.email = request.form['user_email'] user.phone = request.form['user_phone'] user.sign = request.form['user_sign'] user.uuid = str(uuid4().hex) # 获取头像文件,是一个对象(文件名,属性) filestorage = request.files['user_face'] # 头像文件名称 if filestorage.filename != '': fix = '.' + str(filestorage.filename).split('.')[-1] name = str(uuid4().hex) + fix folder = user.uuid try: fname = userfiles.save(storage=filestorage, folder=folder, name=name) # 创建保存压缩图返回文件名 fname_small = create_face(path=os.path.join(app.config['USERS_FILES'], folder), filename=name, base_width=200) fpath = userfiles.path(fname) os.remove(fpath) except Exception as i: print(i) return render_template('404.html') user.face = fname_small """写入""" db.session.add(user) db.session.commit() 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 request.method == 'POST': user_name = request.form['user_name'] user_pwd = request.form['user_pwd'] db = Database(g.db) db_l = db.sql_select(user_name) if db_l is not None: flash('用户名已存在') return render_template('user_regist2.html', form=form) else: db.sql_insert(user_name, user_pwd) flash('注册成功, 请登陆') return redirect(url_for('user_login', name=user_name)) return render_template('user_regist2.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)