def create_user(): nombre = input("Ingrese su nombre: ") apellido = input("Ingrese su apellido: ") email = input("Ingrese su email: ") username = input("Ingrese su username: ") mi_usuario = User(nombre=nombre,apellido=apellido,email=email,username=username) mi_usuario.save()
def reg(): # request.args request.form username = request.values.get('username').strip() password = request.values.get('password').strip() user = User.query.filter_by(username=username).first() if username == '' or password == '': return redirect_with_msg('/reglogin/', "用户名或密码不能为空", 'reglogin') if user is not None: return redirect_with_msg('/reglogin/', "用户名已经存在", 'reglogin') # 更多判断 salt = '.'.join(random.sample('0123456789abcdABCD', 5)) m = hashlib.md5() m.update((password + salt).encode('utf-8')) password = m.hexdigest() user = User(username, password, salt) db.session.add(user) db.session.commit() login_user(user) return redirect('/')
def init_database(): db.drop_all() db.create_all() for i in range(0, 20): db.session.add(User('User' + str(i), 'a' + str(i))) for j in range(0, 10): db.session.add(Image(get_image_url(), i+1)) for k in range(0, 5): db.session.add(Comment('This is a comment ' + str(k), 1+3*i+j, i+1)) db.session.commit()
def init_database(): db.drop_all() db.create_all() for i in range(0, 10): db.session.add(User("User" + str(i + 1), "a" + str(i))) for j in range(0, 10): db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 10): db.session.add( Comment('This is a test LOL' + str(k), 1 + 10 * i + j, i + 1)) db.session.commit()
def init_database(): db.drop_all() db.create_all() # 创建 10 用户 for i in range(0, 10): db.session.add(User('牛客' + str(i), 'a' + str(i))) for j in range(0, 10): # 每人发十张图 db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add( Comment('这是一条评论' + str(k), 1 + 10 * i + j, i + 1)) db.session.commit()
def init_database(): db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('User' + str(i), 'a' + str(i + 1))) for j in range(0, 3): db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add( Comment('This is a comment' + str(k), 1 + 3 * i + j, i + 1)) db.session.commit() # 数据库更新 for i in range(50, 100, 2): user = User.query.get(i) user.username = '******' + user.username User.query.filter_by(id=51).update({'username': '******'}) db.session.commit() # 数据库删除 查出对象删除 for i in range(50, 100, 2): comment = Comment.query.get(i + 1) db.session.delete(comment) db.session.commit() # 数据库的查询 print(1, User.query.all()) print(2, User.query.get(3)) print(3, User.query.filter_by(id=5).first()) print(4, User.query.order_by(User.id.desc()).offset(1).limit(2).all()) print(5, User.query.filter(User.username.endswith('0')).limit(3).all()) print(6, User.query.filter(or_(User.id == 88, User.id == 99)).all()) print(7, User.query.filter(and_(User.id > 88, User.id < 93)).all()) # 分页 print(8, User.query.paginate(page=1, per_page=10).items) # 排序分页 print( 9, User.query.order_by(User.id.desc()).paginate(page=1, per_page=10).items) # 通过用户查images user = User.query.get(1) print(10, user.images) # 通过image知道它的用户是谁 # 在class这个类里面没有user这个属性 # 通过backref在User类中指定images 的backref是User image = Image.query.get(1) print(11, image, image.user)
def reg(): username=request.values.get('username').strip() # 获取用户名 password=request.values.get('password').strip() # 获取密码 if username=='' or password=='': # 判断用户名和密码是不是为空字符串 return render_template('login.html',msg=u'用户名或密码不能为空') user = User.query.filter_by(username=username).first() #查询数据库判断用户名是不是已经存在 if user != None: return render_template('login.html',msg=u'用户名已经存在') # 用户名已经存在 注册失败 salt = '.'.join(random.sample('01234567890abcdefghigABCDEFGHI', 10)) # 生成一个随机字符串 m = hashlib.md5() m.update((password + salt).encode("utf-8")) # 调用md5 哈希函数 生成密文 password = m.hexdigest() user = User(username, password, salt) # 创建用户对象 db.session.add(user) db.session.commit() # 向数据库中插入用户 login_user(user) next = request.values.get('next') # 跳转到原来页面 if next != None and next.startswith('/'): return redirect(next) return redirect('/loadhead/') # 跳转到加载头像页面
def init_database(): db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('User' + str(i + 1), 'a' + str(i))) for j in range(0, 3): db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add(Comment('this is a comment' + str(k), 1 + 3 * i + j, i + 1)) db.session.commit() #删除 for i in range(50,100,2): comment=Comment.query.get(i) db.session.delete(comment) db.session.commit() #更新 # for i in range(0, 100, 2): # u = User.query.get(i) # u.username = '******' # db.session.commit() User.query.filter_by(id=51).update({'username':'******'}) db.session.commit() # 查询 print 1, User.query.get(4) print 2, User.query.filter_by(id=5).first() print 3, User.query.order_by(User.id.desc()).offset(1).limit(2).all() print 4, User.query.filter(User.username.endswith('0')).limit(3).all() print 5, User.query.filter(or_(User.id == 88, User.id == 99)).all() print 6, User.query.filter(and_(User.id > 88, User.id < 93)).all() print 7, User.query.filter(and_(User.id > 88, User.id < 93)).first_or_404() print 8, User.query.order_by(User.id.desc()).paginate(page=1, per_page=10).items user = User.query.get(1) print 9, user.images image = Image.query.get(1) print 10, image.user