Beispiel #1
0
def login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        passwd = request.POST.get('passwd')

        errors = {}
        if not username or len(username.strip()) < 8:
            errors['username'] = [{'message': '用户名不能为空或不能低于8位字符'}]

        if not passwd or len(passwd.strip()) < 8:
            errors['passwd'] = [{'message': '口令不能为空或不能低于8位字符'}]

        if not errors:
            # 验证通过
            qs = UserProfile.objects.filter(
                username=username)  # 返回queryset 查询结果
            if not qs.exists():
                errors['username'] = [{'message': '查无此用户!'}]
            else:
                user = qs.first()  # 读取查询结果中第一条记录
                if not check_password(passwd, user.passwd):
                    errors['passwd'] = [{'message': '口令错误!'}]
                else:
                    helper.addLoginSession(request, user)
                    return redirect('/')

    return render(request, 'user/login.html', locals())
Beispiel #2
0
def login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')

        errors = {}
        if not username or len(username.strip()) < 8:
            errors['username'] = [{'message': '用户名不能为空或不能低于八位字符'}]

        if not password or len(password.strip()) < 8:
            errors['password'] = [{'message': '密码不能为空或不能低于8位'}]

        if not errors:

            qs = UserProfile.objects.filter(username=username)
            if not qs.exists():
                errors['username'] = [{'message': '此用户不存在!'}]
            else:
                user = qs.first()
                if not check_password(password, user.password):
                    errors['password'] = [{'message': '密码错误'}]
                else:
                    helper.addLoginSession(request, user)
                    return redirect('/')
    return render(request, 'user/login.html', locals())
Beispiel #3
0
def regist(request):
    if request.method == 'POST':

        userForm = UserForm(request.POST)
        if userForm.is_valid():
            user = userForm.save()
            helper.addLoginSession(request, user)

            return redirect('/')
        errors = json.loads(userForm.errors.as_json())
        print(errors)
    return render(request, 'user/regist.html', locals())
Beispiel #4
0
def regist(request):
    if request.method == 'POST':
        # 通过ModelForm模型表单类 验证数据并保存到数据库中
        userForm = UserForm(request.POST)
        if userForm.is_valid():  # 判断必要字段是否都有值
            user = userForm.save()  # 保存数据
            # 注册成功,将用户的id,用户名和头像地址写入session(同时将session数据存入redis缓存中)
            helper.addLoginSession(request, user)
            return redirect('/')

        # post提交时有验证错误,将验证错误转成json-->dict对象
        errors = json.loads(userForm.errors.as_json())
        print(errors)
    return render(request, 'user/regist.html', locals())
Beispiel #5
0
def register(request):
    if request.method == 'POST':
        #读取request.POST字典中的数据,借助UserProfile模型保存到数据库
        #通过ModelForm模型表单类,验证数据并保存到数据库中
        userForm = UserForm(request.POST)
        if userForm.is_valid():  #欧安段必要的字段是否都存在数据
            user = userForm.save()  #保存数据
            #注册成功,将用户的id,用户名和头像地址写入到session
            # (同时session数据存入到redis缓存中)
            helper.addLoginSession(request, user)
            return redirect('/')

        #post提交时有验证错误,将错误转成json-->dict对象
        errors = json.loads(userForm.errors.as_json())
        print(errors)
    return render(request, 'user/register.html', locals())