示例#1
0
def login(request):
    if request.method == 'GET':
        form = LoginForm()
        return render(request,'login.html',{'form':form})
    else:
        form = LoginForm(request.POST)
        if form.is_valid():
            # form.cleaned_data#{"username":"******",'password':'******'}
            form.cleaned_data['password'] = md5(form.cleaned_data['password'])
            # user=models.UserInfo.objects.filter(**form.cleaned_data).first()

            user = UserInfo.objects.filter(**form.cleaned_data).first()
            print(user,"user")
            if user:
                ###将用户信息方session
                # permissions_list = user.role.
                #  print(permissions_list, "###################")
                #request.session[settings.USER_SESSION_KEY] ={'id':user.pk,'username':user.username}
                # permissions_list= user.role.fiter(permission__id__isnull=False).values().distinct()
                # 当前用户的所有权限
                init_permissions(user,request)
                return redirect('/user/')
            else:
                form.add_error("password","用户名或密码错误")

        return render(request,'login.html',{'form':form})
示例#2
0
def login(request):
    """
    登陆页
    :param request: 
    :return: 
    """

    if request.method == 'GET':
        obj = LoginForm()
        return render(request, 'login.html', {'obj': obj})
    else:
        obj = LoginForm(request.POST)

        username = request.POST.get('username')
        password = request.POST.get('password')
        code = request.POST.get('code')
        log_time = request.POST.get('log_time')

        if obj.is_valid():
            print(obj.cleaned_data)

            if code.upper() == request.session.get('code').upper():
                user_obj = models.UserInfo.objects.filter(
                    username=username, password=password).first()
                if user_obj:
                    print(user_obj.username)
                    request.session['username'] = username
                    print('ok')
                    print(log_time)
                    # 设置过期时间
                    if log_time:
                        request.session.set_expiry(int(log_time))
                    return redirect('/index.html')
                else:
                    return render(request, 'login.html', {
                        'errormsg': '用户名或密码错误!',
                        'obj': obj
                    })
            else:
                return render(request, 'login.html', {
                    'codemsg': '验证码错误!',
                    'obj': obj
                })
        else:
            return render(request, 'login.html', {'obj': obj})
示例#3
0
def login(request):
    if request.method == 'GET':
        form = LoginForm()
        return render(request, 'login.html', {'form': form})
    else:
        form = LoginForm(request.POST)
        if form.is_valid():
            # form.cleaned_data#{"username":"******",'password':'******'}
            form.cleaned_data['password'] = md5(form.cleaned_data['password'])
            user = UserInfo.objects.filter(**form.cleaned_data).first()
            # print(user,"登录用户")
            if user:
                ###将用户信息放session
                init_permissions(user, request)
                return redirect('/index/')
            else:
                form.add_error("password", "用户名或密码错误")

        return render(request, 'login.html', {'form': form})
示例#4
0
def login(request):
    '登录'
    if request.method == "GET":
        login_form = LoginForm(request)
        return render(request, 'login.html', {'login_form': login_form})
    else:
        login_form = LoginForm(request, request.POST)
        if login_form.is_valid():
            username = login_form.cleaned_data.get('username')
            subtitle = models.UserInfo.objects.filter(
                username=username).values('subtitle').first()['subtitle']
            # 登录成功将用户名写入session中
            request.session['subtitle'] = subtitle
            if request.POST.get('remember_status', False):
                request.session.set_expiry(24 * 60 * 60 * 7)  # 设置一周免登陆
            else:
                request.session.set_expiry(2 * 60 * 60)  # 两个小时免登陆
            return redirect('/mainpage/%s/' % subtitle)
        return render(request, 'login.html', {'login_form': login_form})
示例#5
0
文件: views.py 项目: huboa/xuexi
def login(request):
    if request.method == "GET":
        form = LoginForm()
        return render(request,'login.html',{'form':form})
    else:
        form = LoginForm(request.POST)
        if form.is_valid():
            # form.cleaned_data # {'username':'******','password':'******'}
            # models.UserInfo.objects.filter(username=form.cleaned_data['user'],password=form.cleaned_data['pwd'])
            # models.UserInfo.objects.filter(**{'username':'******','password':123})
            form.cleaned_data['password'] = md5(form.cleaned_data['password'])
            user = models.UserInfo.objects.filter(**form.cleaned_data).first()
            if user:
                # 将用户信息放置到session中
                request.session[settings.USER_SESSION_KEY] = {'id':user.pk,'username':user.username }
                return redirect('/index/')
            else:
                form.add_error('password', '用户名或密码错误')
        return render(request, 'login.html',{'form':form})
示例#6
0
def register1(request):
    if request.method == "GET":
        roles = Role.objects.all()
        form = LoginForm()
        return render(request,'reg.html',{'form':form,"roles":roles})
    else:
        form = LoginForm(request.POST)
        print(form)
        if form.is_valid():
            # form.cleaned_data # {'username':'******','password':'******'}
            # models.UserInfo.objects.filter(username=form.cleaned_data['user'],password=form.cleaned_data['pwd'])
            # models.UserInfo.objects.filter(**{'username':'******','password':123})
            # form.cleaned_data['password'] = md5(form.cleaned_data['password'])
            user = User.objects.filter(**form.cleaned_data).first()
            print(">>>>",user)
            if user:
                # 将用户信息放置到session中
                request.session[settings.USER_SESSION_KEY] = {'id':user.pk,'username':user.username }
                return redirect('/login/')
            else:
                form.add_error('password', '用户名或密码错误')
        return render(request, 'reg.html',{'form':form})
示例#7
0
def login(request):
    '登录功能'
    if request.method == "GET":
        login_form = LoginForm(request)
        return render(request, 'login.html', {'login_form': login_form})
    else:
        login_form = LoginForm(request, request.POST)
        if login_form.is_valid():
            # 登录成功将subtitle写入session中
            subtitle = models.UserInfo.objects.filter(
                username=login_form.cleaned_data.get('username')).values(
                    'blog__subtitle').first()
            request.session['subtitle'] = subtitle.get('blog__subtitle')
            # 登录成功返回到博客网主页
            if request.POST.get('remember_status', False):
                request.session.set_expiry(24 * 60 * 60 * 7)  # 设置一周免登陆
            else:
                request.session.set_expiry(2 * 60 * 60)  # 两个小时免登陆
            return redirect(
                '/blog_mainpage/' +
                subtitle.get('blog__subtitle'))  # 如果redirect一个URL,该URL包含用户名即可
        return render(request, 'login.html', {'login_form': login_form})
示例#8
0
def login(request):
    if request.method == "GET":
        form = LoginForm()
        return render(request, "login.html", {"form": form})

    else:
        # 将接收到的数据,传给LoginForm()类 通过form 验证;
        form = LoginForm(request.POST)
        # 通过 form 验证后,form 为True
        if form.is_valid():
            # 通过form验证的数据 存放在 cleaned_data 中
            # form.cleaned_data

            # 通过自定义 md5 模块验证密码
            form.cleaned_data["password"] = md5(form.cleaned_data["password"])

            # 通过 **form.cleaned_data 可以将 数据依照字典形式获取 filter({"username":zhangsan,"password":1234})
            userinfo = models.UserInfo.objects.filter(
                **form.cleaned_data).first()  # 拿取第一个对象值

            # 如果userinfo 中有数据,即表示验证成功
            if userinfo:
                # 将 用户信息 放置到 session 中
                request.session[settings.USER_SESSION_KEY] = {
                    "id": userinfo.pk,
                    "username": userinfo.username
                }

                # 重定向 页面
                return redirect("/index/")
            else:
                # 用户验证失败
                form.add_error("password", "用户名或密码错误")

        # 存放错误信息 form.errors

        # 将 form 清洗过的 数据直接返回给 html 模板
        return render(request, "login.html", {"form": form})
示例#9
0
def login(request):
    """
    用户登陆
    :param request:
    :return:
    """
    if request.method == "GET":
        obj = LoginForm()
        return render(request, "login.html", {"obj": obj})
    else:
        obj = LoginForm(request.POST)
        input_code = request.POST.get("code")
        session_code = request.session.get("code")
        if input_code.upper() == session_code.upper():
            if obj.is_valid():
                row = models.UserInfo.objects.filter(
                    username=obj.cleaned_data["username"],
                    password=obj.cleaned_data["password"]).first()
                if row:
                    request.session["username"] = obj.cleaned_data["username"]
                    request.session["password"] = obj.cleaned_data["password"]
                    request.session["id"] = row.nid
                    print(row.nid)
                    print(request.session.get("id"))
                    print(request.session)
                    return redirect("/home/")
                else:
                    # pass
                    return render(request, "login.html", {
                        "obj": obj,
                        "error_msg": "用户名或者密码错误"
                    })
        else:
            return render(request, "login.html", {
                "obj": obj,
                "code_error": "验证码错误"
            })
示例#10
0
def login(request):
    """
    用户登录,登录完成后将这个用户的session 传给index
    :param session:用户的登录名,用于查询发布的文章
    :return:
    """
    print('request loai')
    if request.method == 'GET':
        obj = LoginForm()

        return render(request, 'login.html', {'obj': obj})

    else:
        session_code = request.session.get('code')

        input_code = request.POST.get('check_code')

        obj = LoginForm(request.POST)

        if input_code.upper() == session_code.upper():

            # print(obj.errors, obj.cleaned_data)

            print(obj.is_valid())

            print('check exec')

            if obj.is_valid():
                print('hello databases')

                user_result = models.UserInfo.objects.filter(
                    **obj.cleaned_data)
                if user_result:
                    print('user_result true')

                    request.session['username'] = obj.cleaned_data.get(
                        'username')

                    # 后台管理权限设置 -------------------------------------------------------------------------
                    user_obj = usermanager.User.objects.filter(
                        username=obj.cleaned_data.get('username')).first()

                    # print(user_obj.id)

                    role_obj = usermanager.Role.objects.filter(
                        users__user_id=user_obj.id).values('id')
                    print(role_obj)
                    # x = usermanager.User2Role.objects.filter(user__id=user_obj.id) #
                    # x拿到的是<QuerySet [<User2Role: tony-市场部>, <User2Role: tony-售后>]>
                    # role_obj <QuerySet [<Role: 市场部>, <Role: 售后>]>

                    #经过上两个步骤 这个时候可以拿到这个用户的 权限-操作-角色 三个表关联对对象,通过这个对象可以拿到这个用户可以有什么权限、有什么
                    # permission_list=usermanager.Permission2Action2Role.objects.filter(role_id__in=role_obj.id).annotate(ct=Count('id'))
                    # print(permission_list)

                    permission_list = usermanager.Permission2Action2Role.objects.filter(
                        role_id__in=role_obj).annotate(ct=Count('id')).values(
                            'role_id', 'permission_id', 'permission__url',
                            'action__code').distinct()
                    # print(permission_list)
                    # {'permission_id': 1, 'role_id': 1, 'permission__url': '/user.html', 'action__code': 'post'}
                    # {'permission_id': 3, 'role_id': 4, 'permission__url': '/blogs.html', 'action__code': 'post'}
                    user_permission_dict = {}

                    for k in permission_list:
                        user_permission_dict[k['permission__url']] = [
                            k['action__code']
                        ]

                    request.session[
                        'user_permission_dict'] = user_permission_dict
                    print(request.session['user_permission_dict'], '3333')

                    # 后台管理权限设置 -------------------------------------------------------------------------

                    return index(request)
                else:
                    return render(request, 'login.html', {
                        'obj': obj,
                        'msg': '用户名或密码错误'
                    })
            else:
                return render(request, 'login.html', {'obj': obj})
        else:
            print('i"exc ')
            return render(request, 'login.html', {
                'obj': obj,
                'checkcode': '验证码不正确'
            })