コード例 #1
0
ファイル: account.py プロジェクト: viking-h/Django-blog
def login(request):
    """
    登录函数
    :param request:
    :return:
    """
    if request.method == 'GET':
        return render(request, 'web_home/login.html')
    elif request.method == 'POST':
        result = {'status': False, 'message': None, 'data': None}
        login_form = LoginForm(request=request, data=request.POST)
        if login_form.is_valid():
            username = login_form.cleaned_data.get('username')
            password = login_form.cleaned_data.get('password')
            user_info = UserInfo.objects.filter(username=username, password=password)\
                .values(
                'nid', 'nickname', 'username', 'email',
                'avatar', 'blog__nid', 'blog__site'
            ).first()
            if not user_info:
                result['message'] = '用户名或密码错误'
            else:
                result['status'] = True
                request.session['user_info'] = user_info
                if login_form.cleaned_data.get('rmeb') == True:
                    request.session.set_expiry(60 * 60 * 24 * 30)
        else:
            print(login_form.errors)
            if 'check_code' in login_form.errors:
                result['message'] = '验证码错误'
            else:
                result['message'] = '用户名或密码错误'
        return HttpResponse(json.dumps(result))
コード例 #2
0
def login(req):
    '''
    用户登录 并且在登录后初始化各个表数据
    :param req:
    :return:
    '''

    rep = BaseResponse()
    form = LoginForm(req.POST)
    if form.is_valid():
        _value_dict = form.clean()
        # print(_value_dict)
        if _value_dict['code'].lower() != req.session['CheckCode'].lower():
            rep.message = {'code': [{'message': '验证码错误'}]}
            return HttpResponse(json.dumps(rep.__dict__))
        '''
        如果验证码正确,则进行Q查询
        '''
        from django.db.models import Q
        con = Q()
        q1 = Q()
        q2 = Q()

        q1.connector = 'AND'  # 在q1中创建逻辑关系符 ,然后把数据库中的值与用户输入的值进行比较
        q1.children.append(('email', _value_dict['user']))
        q1.children.append(('password', _value_dict['pwd']))

        q2.connector = 'AND'
        q2.children.append(('username', _value_dict['user']))
        q2.children.append(('password', _value_dict['pwd']))

        con.add(q1, 'OR')
        con.add(q2, 'OR')

        obj = models.UserInfo.objects.filter(con).first()

        if not obj:
            rep.message = {'user': [{'message': '用户名或邮箱或密码错误!'}]}
            return HttpResponse(json.dumps(rep.__dict__))

        req.session['is_login'] = True
        req.session['user_info'] = {
            'nid': obj.nid,
            'email': obj.email,
            'username': obj.username
        }

        rep.status = True
        # 表检查
        table_verify(obj)
    else:
        error_msg = form.errors.as_json()
        rep.message = json.loads(error_msg)

    return HttpResponse(json.dumps(rep.__dict__))
コード例 #3
0
ファイル: account.py プロジェクト: w7374520/Trainning
def login(request):
    """
    用户登陆
    :param request:
    :return:
    """
    rep = BaseResponse()
    form = LoginForm(request.POST)
    if form.is_valid():
        _value_dict = form.clean()
        if _value_dict['code'].lower() != request.session["CheckCode"].lower():
            rep.message = {'code': [{'message': '验证码错误'}]}
            return HttpResponse(json.dumps(rep.__dict__))

        from django.db.models import Q

        con = Q()
        q1 = Q()
        q1.connector = 'AND'
        q1.children.append(('email', _value_dict['user']))
        q1.children.append(('password', _value_dict['pwd']))

        q2 = Q()
        q2.connector = 'AND'
        q2.children.append(('username', _value_dict['user']))
        q2.children.append(('password', _value_dict['pwd']))

        con.add(q1, 'OR')
        con.add(q2, 'OR')

        obj = models.UserInfo.objects.filter(con).first()
        if not obj:
            rep.message = {'user': [{'message': '用户名邮箱或密码错误'}]}
            return HttpResponse(json.dumps(rep.__dict__))

        request.session['is_login'] = True
        request.session['user_info'] = {
            'nid': obj.nid,
            'email': obj.email,
            'username': obj.username
        }
        rep.status = True
    else:
        error_msg = form.errors.as_json()
        rep.message = json.loads(error_msg)

    return HttpResponse(json.dumps(rep.__dict__))
コード例 #4
0
def login(request):
    """
    用户名和密码登录
    :param request:
    :return:
    """
    if request.method == "GET":
        form = LoginForm(request)
        return render(request, 'login.html', {'form': form})
    form = LoginForm(request, data=request.POST)
    if form.is_valid():
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']

        # user_object = models.UserInfo.objects.filter(username=username, password=password).first()

        user_object = models.UserInfo.objects.filter(
            Q(username=username) | Q(email=username) | Q(mobile_phone=username)).filter(
            password=password).first()

        if user_object:
            # 登录成功
            request.session['user_id'] = user_object.id
            request.session.set_expiry(60 * 60 * 24 * 7)

            return redirect('index')
        form.add_error('username', '用户名密码错误')

    return render(request, 'login.html', {'form': form})
コード例 #5
0
ファイル: account.py プロジェクト: Bean-jun/SimpleSaaS
def login(request):
    """用户名和密码登录"""
    if request.method == "GET":
        form = LoginForm(request)
        return render(request, 'web/login.html', {'form': form})

    if request.method == "POST":
        # 获取数据并校验
        form = LoginForm(request, request.POST)

        if form.is_valid():
            # 验证成功,登录
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']

            # user = models.UserInfo.objects.filter(username=username, password=password).first()
            # 用户使用邮箱、手机号均可以实现登录
            user = models.UserInfo.objects.\
                filter(Q(email=username)|Q(mobile_phone=username)).filter(password=password).first()

            if user:
                # 用户账号密码正确
                # 登录成功写入session中
                request.session['user_id'] = user.id
                request.session.set_expiry(60 * 60 * 24 * 7)

                return redirect(reverse('web:index'))
            else:
                form.add_error('username', "用户名或密码错误")

        return render(request, 'web/login.html', {'form': form})
コード例 #6
0
def login(request):
    """  用户名和密码登录 """
    if request.method == 'GET':
        form = LoginForm(request)
        return render(request, 'login.html', {'form': form})

    form = LoginForm(request, data=request.POST)
    if form.is_valid():
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']

        # user_object = models.UserInfo.objects.filter(username=username, password=password).first()
        # ( 手机=手机 and pwd = pwd )or (邮箱 = 邮箱 and pwd = pwd)
        from django.db.models import Q
        user_object = models.UserInfo.objects.filter(
            Q(email=username)
            | Q(mobile_phone=username)).filter(password=password).first()

        if user_object:
            # 用户名密码正确
            request.session['user_id'] = user_object.id
            # 用户信息保留两周时间
            request.session.set_expiry(60 * 60 * 24 * 14)

            return redirect('index')
        form.add_error('username', '用户名或密码错误')

    return render(request, 'login.html', {'form': form})
コード例 #7
0
def login(request):
    """ 用户名和密码登录 """
    if request.method == 'GET':
        form = LoginForm(request)
        return render(request, 'login.html', {'form': form})
    form = LoginForm(request, data=request.POST)
    if form.is_valid():
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        password = 123
        # user_object = models.UserInfo.objects.filter(username=username, password=password).first()
        #  (手机=username and pwd=pwd) or (邮箱=username and pwd=pwd)

        user_object = models.UserInfo.objects.filter(
            Q(email=username)
            | Q(mobile_phone=username)).filter(password=password).first()
        print(user_object)
        if user_object:
            # 登录成功为止1
            request.session['user_id'] = user_object.id
            request.session.set_expiry(60 * 60 * 24 * 14)

            return redirect('index')

        form.add_error('username', '用户名或密码错误')

    return render(request, 'login.html', {'form': form})
コード例 #8
0
def login(request):
    """
    输入用户名和密码登录
    :return:
    """
    if request.method == "GET":
        form = LoginForm(request)
        return render(request, "login.html", {"form": form})
    form = LoginForm(request, data=request.POST)
    if form.is_valid():
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        # 用户名和密码的登录
        # user_object = models.UserInfo.objects.filter(username=username, password=password).first()
        # (手机=username and pwd=pwd) or (邮箱=username and pwd=pwd)
        user_object = models.UserInfo.objects.filter(
            Q(email=username)
            | Q(mobile_phone=username)).filter(password=password).first()
        if user_object:
            # 用户名密码正确,登录成功
            request.session["user_id"] = user_object.id
            request.session.set_expiry(60 * 60 * 24 * 14)  # 2周
            return redirect('index')
        form.add_error('username', '用户名或密码错误')
    return render(request, "login.html", {"form": form})
コード例 #9
0
 def post(self, request, *args, **kwargs):
     form = LoginForm(request, data=request.POST)
     if form.is_valid():
         username = form.cleaned_data.get('username')
         password = form.cleaned_data.get('password')
         from django.db.models import Q
         # 验证成功,将用户ID写入session中
         user_object = models.UserInfo.objects.filter(
             Q(mobile_phone=username)
             | Q(email=username)).filter(password=password).first()
         if user_object:
             request.session['user_id'] = user_object.id
             request.session.set_expiry(60 * 60 * 24 * 14)
             return redirect('index')
         else:
             form.add_error(username, '用户名或者密码错误')
             return render(request, 'login.html', {'form': form})
     else:
         return render(request, 'login.html', {'form': form})
コード例 #10
0
def login(request):
    """
    登入
    :param request:
    :return:
    """
    if request.method == "GET":
        obj = LoginForm(request)
        return render(request, "login.html", locals())
    else:
        result = {'status': False, 'message': None, 'data': None}
        obj = LoginForm(request, request.POST)
        if obj.is_valid():
            print(obj.cleaned_data)
            username = obj.cleaned_data.get("username")
            password = obj.cleaned_data.get("password")
            user_info = models.UserInfo.objects.filter(
                username=username,
                password=password).values('nid', 'nickname', 'username',
                                          'email', 'avatar', 'blog__nid',
                                          'blog__site').first()
            if user_info:
                request.session["user_info"] = user_info
                print(request.session["user_info"])
                result["data"] = user_info
                result["status"] = True
                return redirect("/")
            else:
                result["message"] = "帳號密碼錯誤"
                obj.errors["username"] = ("帳號密碼錯誤", )
                return render(request, "login.html", locals())
        else:
            print(obj.errors)
            result["message"] = obj.errors
            return render(request, "login.html", locals())
コード例 #11
0
ファイル: account.py プロジェクト: daibaiyang119/chouti
def login(request):
    """用户登录"""
    rep = BaseResponse()
    form =LoginForm(request.POST)
    if form.is_valid():
        # 先检查图形验证码是否匹配
        _value_dict = form.clean()
        if _value_dict["code"].lower() != request.session["CheckCode"].lower():
            rep.message = {"code": [{"message": "验证码错误"}]}

        # 如果验证码正确,数据库中匹配用户名&密码或邮箱&密码
        con = Q()
        q1 = Q()
        q1.connector = "AND"
        q1.children.append(("email", _value_dict["user"]))
        q1.children.append(("password", _value_dict["pwd"]))

        q2 = Q()
        q2.connector = "AND"
        q2.children.append(("username", _value_dict["user"]))
        q2.children.append(("password", _value_dict["pwd"]))

        con.add(q1, "OR")
        con.add(q2, "OR")

        obj = models.UserInfo.objects.filter(con).first()
        if not obj:
            rep.message = {"user": [{"message": "用户名邮箱或密码错误"}]}
            return HttpResponse(json.dumps(rep.__dict__))

        # 如果验证正确,设置session
        request.session["is_login"] = True
        request.session["user_info"] = {'nid': obj.nid, 'email': obj.email, 'username': obj.username}

        rep.status = True
    else:
        error_str = form.errors.as_json()
        rep.message = json.loads(error_str)
    return HttpResponse(json.dumps(rep.__dict__))
コード例 #12
0
ファイル: account.py プロジェクト: guoxing126/smart_manager
def login(request, *args, **kwargs):
    if request.method == 'GET':
        return render(request, 'login.html')
    else:
        log_form = LoginForm(request=request, data=request.POST)
        if log_form.is_valid():
            username = log_form.cleaned_data.get('username')
            password = log_form.cleaned_data.get('password')
            user_info = models.UserInfo.objects.filter(username=username, password=password). \
                values('nid', 'nickname',
                       'username', 'email',
                       'avatar',
                       'blog__nid',
                       'blog__site').first()
            # print(user_info)
            request.session['user_info'] = user_info
            print(request.session['user_info'])
            # if form.cleaned_data.get('rmb'):
            #     request.session.set_expiry(60 * 60 * 24 * 30)
            return redirect('/')
        else:
            print(log_form.errors)
            return render(request, 'login.html', {'log_form': log_form})
コード例 #13
0
ファイル: account.py プロジェクト: Eric54920/BugTracer
def login(request):
    if request.method == 'GET':
        form = LoginForm(request)
        return render(request, 'login.html', {'form': form})
    form = LoginForm(request, data=request.POST)
    if form.is_valid():
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        user_object = models.UserInfo.objects.filter(
            Q(email=username)
            | Q(username=username)).filter(password=password).first()
        if user_object:
            request.session['user_id'] = user_object.id
            request.session.set_expiry(60 * 60 * 24 * 14)
            return redirect('web:index')
        form.add_error('username', '用户名或密码错误')
    return render(request, 'login.html', {'form': form})
コード例 #14
0
ファイル: account.py プロジェクト: wanglifeng1/tracer
def login(request):
    ''' 用户名密码登陆 '''
    if request.method == "GET":
        form = LoginForm(request)
        return render(request, 'web/login.html', {"form": form})
    form = LoginForm(request, data=request.POST)
    if form.is_valid():
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')
        # 邮箱或者手机号登陆 或 用户名/密码登陆
        user_obj = models.UserInfo.objects.filter(
            Q(mobile_phone=username)
            | Q(email=username)).filter(password=password).first()
        if user_obj:
            request.session['user_id'] = user_obj.id
            request.session.set_expiry(60 * 60 * 24 * 14)
            return redirect('web:index')
        form.add_error("username", "用户名或密码错误")
    return render(request, 'web/login.html', {"form": form})
コード例 #15
0
ファイル: account.py プロジェクト: Lucareful/tracker
def login(request):
    """ 用户名和密码登录 """
    if request.method == "GET":
        form = LoginForm(request)
        return render(request, "login.html", {"form": form})
    form = LoginForm(request, data=request.POST)
    if form.is_valid():
        username = form.cleaned_data["username"]
        password = form.cleaned_data["password"]

        user_object = (models.UserInfo.objects.filter(
            Q(mail=username)
            | Q(mobilePhone=username)).filter(password=password).first())
        if user_object:
            # 登录成功为止0
            request.session["user_id"] = user_object.id
            request.session.set_expiry(59 * 60 * 24 * 14)

            return redirect("index")

        form.add_error("username", "用户名或密码错误")

    return render(request, "login.html", {"form": form})
コード例 #16
0
ファイル: account.py プロジェクト: luffy-org/s0405
def login(request):
    """
    账号登陆
    :param request:
    :return:
    """
    if request.method == 'GET':
        form = LoginForm(request)
        return render(request, 'login.html', {'form': form})
    form = LoginForm(request, data=request.POST)
    if form.is_valid():
        pwd = form.cleaned_data.get('pwd')
        user = form.cleaned_data.get('username')
        # user_obj = UserInfo.objects.filter(username=user, password=pwd).first()
        user_obj = UserInfo.objects.filter(
            Q(email=user, password=pwd)
            | Q(mobile_phone=user, password=pwd)).first()
        if user_obj:
            request.session['user_id'] = user_obj.id
            # 设置过期时间
            return redirect('/index/')
        form.add_error('pwd', '用户名密码错误')
    return render(request, 'login.html', {'form': form})
コード例 #17
0
 def get(self, request, *args, **kwargs):
     form = LoginForm(request)
     return render(request, 'login.html', {'form': form})