コード例 #1
0
def register():
    if request.method == 'POST':
        username = request.form.get('username', '').strip()
        password1 = request.form.get('password1', '').strip()
        password2 = request.form.get('password2', '').strip()
        gender = request.form.get('gender', '').strip()
        city = request.form.get('city', '').strip()
        phone = request.form.get('phone', '').strip()
        now = datetime.datetime.now()
        try:
            User.query.filter_by(username=username).one()
            return render_template('register.html', err='用户名已存在')
        except Exception:
            if not password1 or password1 != password2:
                return render_template('register.html', err='密码不一致')

            user = User(username=username,
                        password=make_password(password1),
                        gender=gender,
                        city=city,
                        phone=phone,
                        create_time=now)

            # 保存头像
            avatar_file = request.files.get('avatar', '')
            if avatar_file:
                user.avatar = save_avatar(avatar_file)

            db.session.add(user)
            db.session.commit()
            return redirect('/user/login')

    else:
        return render_template('register.html')
コード例 #2
0
def register():
    if request.method == 'POST':
        username = request.form.get('username', '').strip()
        password1 = request.form.get('password1', '').strip()
        password2 = request.form.get('password2', '').strip()
        gender = request.form.get('gender', '').strip()
        birthday = request.form.get('birthday', '').strip()
        city = request.form.get('city', '').strip()
        bio = request.form.get('bio', '').strip()
        now = datetime.datetime.now()

        if not password1 or password1 != password2:
            return render_template('register.html', err='密码不符合要求')

        user = USER(username=username, password=make_password(password1), gender=gender, birthday=birthday, city=city,
                    bio=bio, created=now)

        avatar_file = request.files.get('avatar')
        if avatar_file:
            user.avatar = save_avatar(avatar_file)
        try:
            db.session.add(user)
            db.session.commit()
            return redirect('/user/login')
        except IntegrityError:
            db.session.rollback()
            return render_template('register.html', err='昵称被占用')
    else:
        return render_template('register.html')
コード例 #3
0
ファイル: views.py プロジェクト: vigor025100/weibo
def register():
    # 新用户注册信息提交
    if request.method == 'POST':
        username = request.form.get('username', '').strip()
        password1 = request.form.get('password1', '').strip()
        password2 = request.form.get('password2', '').strip()
        gender = request.form.get('gender', '').strip()
        city = request.form.get('city', '').strip()
        address = request.form.get('address', '').strip()
        phone = request.form.get('phone', '').strip()
        birthday = request.form.get('birthday', '').strip()

        # 验证两次输入的密码是否一致
        if not password1 or password1 != password2:
            return render_template('register.html', err='两次输入密码不相符')

        user = User(username=username,
                    password=make_password(password1),
                    gender=gender,
                    city=city,
                    address=address,
                    phone=phone,
                    birthday=birthday)
        # username 设置了唯一的属性,当用户名重复的时候,注册信息添加到数据库会报错
        try:
            db.session.add(user)  # 用户信息写入数据库
            db.session.commit()  # 提交数据
            return redirect('/user/login')
        except IntegrityError:
            db.session.rollback
            return render_template('register.html', err='用户名已被占用')

    # 拉起注册页面
    else:
        return render_template('register.html')
コード例 #4
0
def modify():
    '''修改用户信息'''
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        gender = request.form.get('gender')
        phone = request.form.get('phone')
        city = request.form.get('city')
        hobbit = request.form.get('hobbit')
        des = request.form.get('des')
        if not password:
            return render_template('response.html', msg='密码不能为空')

        photo = request.files.get('photo')
        if photo:
            photo = save_avatar(photo)
        else:
            photo = User.query.filter_by(username=username).one().photo
        old_user = User.query.filter_by(username=username)
        old_user.update({
            'username': username,
            'password': make_password(password),
            'gender': gender,
            'phone': phone,
            'photo': photo,
            'city': city,
            'hobbit': hobbit,
            'des': des
        })
        db.session.commit()
        return render_template('response.html', msg='修改成功')
    else:
        username = session['username']
        user = User.query.filter_by(username=username).one()
        return render_template('modify.html', user=user)
コード例 #5
0
ファイル: views.py プロジェクト: sept44/weibo
def register():
    '''注册'''
    if request.method == 'POST':
        nickname = request.form.get('nickname', '').strip()
        password = request.form.get('password', '').strip()
        gender = request.form.get('gender', 'unknow').strip()
        city = request.form.get('city', '上海').strip()
        avatar = request.files.get('avatar')
        birthday = request.form.get('birthday', '2000-01-01').strip()
        bio = request.form.get('bio', '').strip()

        if not (nickname and password):
            return render_template('register.html', error='昵称或密码不能为空')

        safe_password = utils.make_password(password)  # 安全处理密码
        avatar_url = utils.save_avatar(nickname, avatar)  # 保存头像,并返回头像网址

        user = User(nickname=nickname, password=safe_password, gender=gender,
                    city=city, avatar=avatar_url, birthday=birthday, bio=bio)

        db.session.add(user)
        try:
            db.session.commit()
        except IntegrityError:
            db.session.rollback()  # 操作失败,进行事务回滚
            return render_template('register.html', error='昵称或密码不能为空')
        return redirect('/user/login')
    else:
        return render_template('register.html')
コード例 #6
0
def register():
    if request.method == "GET":
        return render_template('register.html')
    else:
        username = request.form.get('username')
        password = request.form.get('password')
        password2 = request.form.get('password2')
        tel = request.form.get('tel')
        birthday = request.form.get('birthday')
        gender = request.form.get('gender')
        now = datetime.datetime.now()
        avatar = request.files.get('avatar')

        try:
            Users.query.filter_by(username=username).one()
            return render_template('register.html', error=2)
        except NoResultFound:
            if not password or password != password2:
                return render_template('register.html', error=1)
            else:
                u1 = Users(username=username,
                           password=make_password(password),
                           tel=tel,
                           birthday=birthday,
                           gender=gender,
                           created=now)

                if avatar:
                    u1.avatar = save_avatar(avatar)

                db.session.add(u1)
                db.session.commit()

            return redirect('/user/login')
コード例 #7
0
def register():
    if request.method == 'POST':
        nickname = request.form.get('', '').strip()
        password1 = request.form.get('', '').strip()
        password2 = request.form.get('', '').strip()
        gender = request.form.get('', '').strip()
        birthday = request.form.get('', '').strip()
        city = request.form.get('', '').strip()
        bio = request.form.get('', '').strip()
        now = datetime.datetime.now()

        if not password1 or password1 != password2:
            return render_template('register.html', err='密码不符合要求')
        user = User(nickname=nickname,
                    gender=gender,
                    created=now,
                    birthday=birthday,
                    city=city,
                    bio=bio,
                    password=make_password(password1))

        # 保存头像
        avatar = request.file.get('')

        db.session.add(user)
        db.session.commit()

    else:
        return render_template('register.html')
コード例 #8
0
def register():
    if request.method == 'POST':
        #get里面没有的空单引号'' 的意义是 如果你没有写里面内容的话,默认是''
        # strip() 的作用是把字符串两边的空白符去掉,但是不会去掉字符串内部的空白符

        nickname = request.form.get('nickname', '').strip()
        password1 = request.form.get('password1', '').strip()
        password2 = request.form.get('password1', '').strip()
        gender = request.form.get('gender', '').strip()
        birthday = request.form.get('birthday', '').strip()
        city = request.form.get('city', '').strip()
        bio = request.form.get('avatar', '').strip()
        now = datetime.datetime.now()  #注册时间

        if not password1 or password1 != password2:
            return render_template('register.html', err='密码不符合要求')

        user = User(
            nickname=nickname,
            password=make_password(password1),
            gender=gender,
            birthday=birthday,
            city=city,
            bio=bio,
            created=now
        )

        #保存头像
        avatar_file = request.files.get('avatar')
        if avatar_file:
            # print(dir(avatar_file.stream))
            # avatar_url = save_avatar(avatar)
            # user.avatar = avatar_url
            user.avatar = save_avatar(avatar_file)

        try:
            #保存到数据库
            db.session.add(user)
            db.session.commit()
            return redirect('/user/login')
        except IntegrityError:
            db.session.rollback()
            # print(e)
            return render_template('register.html', err='您的昵称已被占用')
    else:
        return render_template('register.html')
コード例 #9
0
def register():
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        city = request.form.get('city')
        phone = request.form.get('phone')

        user = User(username=username,
                    password=make_password(password),
                    city=city,
                    phone=phone)

        db.session.add(user)
        db.session.commit()
        # 如果用户名冲突的话 需要处理 此处没有处理
        return render_template('login.html')
    else:
        return render_template('register.html')
コード例 #10
0
def update():
    uid = session.get('uid')
    user_info = Users.query.get(uid)

    if request.method == "GET":
        return render_template('update.html', user_info=user_info)
    else:
        old_password = request.form.get('old_password')
        new_password = request.form.get('new_password')
        new_tel = request.form.get('tel')

        if check_password(old_password, user_info.password) is False:
            return render_template('update.html', error=1, user_info=user_info)
        else:
            user_info.password = make_password(new_password)
            user_info.tel = new_tel
            db.session.commit()
            session.clear()
        return redirect('/')
コード例 #11
0
def register():
    if request.method == 'POST':
        nickname = request.form.get('nickname')
        password = request.form.get('password')
        birthday = request.form.get('birthday')

        user = User(nickname=nickname,
                    password=make_password(password),
                    birthday=birthday)

        try:
            # 保存到数据库
            db.session.add(user)
            db.session.commit()
            return redirect('/user/')
        except IntegrityError:
            db.session.rollback()
            return render_template('register.html', err='您的昵称已被占用')
    else:
        return render_template('register.html')
コード例 #12
0
def register():
    '''用户注册'''
    if request.method == 'POST':
        username = request.form.get('username', '').strip()
        password = request.form.get('password', '').strip()
        pwd = request.form.get('pwd', '').strip()
        gender = request.form.get('gender')
        phone = request.form.get('phone', '').strip()
        check = User.query.filter_by(username=username).count()
        if password != pwd:
            return render_template('response.html', msg='两次密码输入不一致')
        if check != 0:
            return render_template('response.html', msg='用户名已存在')
        user = User(username=username,
                    password=make_password(password),
                    gender=gender,
                    phone=phone)
        db.session.add(user)
        db.session.commit()
        return redirect('/')
    else:
        return render_template('index.html')