Ejemplo n.º 1
0
def user_login(request):
    # 如果登录是从其他页面跳转过来的,会带next参数,如果有next参数,登录完成后需要跳转到next所对应的地址,否则跳转到首页
    next_url = request.GET.get('next', 'index')
    # 第一次访问URL GET
    # 第二次访问URL POST
    if request.method == 'POST':
        form = UserLoginForm(request=request, data=request.POST)
        client = VerifyCode(request)
        code = request.POST.get('vcode', None)
        rest = client.validate_code(code)
        print('验证结果:', rest)

        if form.is_valid():
            data = form.cleaned_data
            # 使用自定义的方式实现登录
            # user = User.objects.get(username=data['username'], password=data['password'])
            # request.session[constants.LOGIN_SESSION_ID] = user.id
            # return redirect('index')

            # 使用django-auth实现登录
            user = authenticate(request, username=data['username'], password=data['password'])
            if user is not None:
                login(request, user)
                return redirect(next_url)
        else:
            print(form.errors)
    else:
        form = UserLoginForm(request)
    return render(request, 'login.html', {
        'form': form,
        'next_url': next_url
    })
Ejemplo n.º 2
0
def user_login(request):
    # 第一次访问为GET
    # 第二次访问为POST
    if request.method == 'POST':
        form = UserLoginForm(request=request, data=request.POST)
        print(request.POST)
        client = VerifyCode(request)
        code = request.POST.get('verify_code', None)
        print(type(code))
        rest = client.validate_code(code)
        print('验证结果', rest)
        # 验证表单是否通过
        print(form.is_valid())
        if form.is_valid():
            # 执行登陆
            data = form.cleaned_data

            ## 使用自定义的方式实现登陆
            # 查询用户信息 [MD5]加密算法,不可逆的加密算法1243 -> sdfasvdc
            user = User.objects.get(username=data['username'],
                                    password=data['password'])
            # 设置用户ID到session
            request.session[constants.LOGIN_SESSION_ID] = user.id
            # 登陆后的跳转
            return redirect('analysis:index')

        else:
            print(form.errors)
    else:
        form = UserLoginForm(request)
    return render(request, 'login.html', {'form': form})
Ejemplo n.º 3
0
    def post(self, request):
        user = request.user
        data = {'status': 0, 'msg': '发送失败,请重试'}
        code = request.POST.get('txtCode')
        v_code = VerifyCode(request)
        if v_code.validate_code(code):
            to_username = request.POST.get('txtUserName')
            to_user = User.objects.filter(username=to_username).first()
            if not to_user or to_user.username == 'system_admin':
                data = {'status': 0, 'msg': '沙雕,没有这个用户'}
                return JsonResponse(data)
            title = request.POST.get('txtTitle')
            content = request.POST.get('txtContent')
            UserMessage.objects.create(user=user,
                                       title=title,
                                       content=content,
                                       to_user=to_user.id)
            UserMessageAccept.objects.create(user=user,
                                             title=title,
                                             content=content,
                                             to_user=to_user.id)
            data = {'status': 1, 'msg': '已发送'}
        else:
            data = {'status': 0, 'msg': '验证码输错辣'}

        return JsonResponse(data)
Ejemplo n.º 4
0
def user_login(request):
    """用户登录"""
    next_url = request.GET.get("next", "index")

    if request.method == "POST":
        form = UserLoginForm(request=request, data=request.POST)
        client = VerifyCode(request)
        code = request.POST.get("vcode", None)
        rest = client.validate_code(code)

        # 检验表单是否通过验证
        if form.is_valid():
            data = form.cleaned_data
            user = authenticate(request,
                                username=data["username"],
                                password=data["password"])
            if user:
                login(request, user)
                request.session["user_id"] = user.id
                return redirect(next_url)
        else:
            print(form.errors)
    else:
        form = UserLoginForm(request)
    return render(request, "login.html", {
        "form": form,
        "next_url": next_url,
    })
Ejemplo n.º 5
0
 def clean_verify_code(self):
     # 验证码的验证
     verify_code = self.cleaned_data['verify_code']
     if not verify_code:
         raise forms.ValidationError('请输入验证码')
     client = VerifyCode(self.request)
     if not client.validate_code(verify_code):
         raise forms.ValidationError('输入的验证码不正确')
Ejemplo n.º 6
0
def user_login(request):
    """用户登录"""
    # 如果登录是从其他页面跳转过来的,会带next参数,如果有next参数,登录完成后,需要跳转到
    # next所对应的地址,否则,跳转到首页上去
    # 一开始访问的时候就定义好这个地址
    next_url = request.GET.get('next', 'index')
    # 第一次访问URL,GET请求,展示表单,供用户输入
    # 第二次访问URL,POST请求
    if request.method == 'POST':
        # 实例化UserLoginForm对象时,需要传request参数,
        # 原来实例化UserLoginForm对象
        # form = UserLoginForm(request.POST),request.POST其实是data
        form = UserLoginForm(request=request, data=request.POST)
        print(request.POST)
        client = VerifyCode(request)
        code = request.POST.get('vcode', None)
        print(code)
        rest = client.validate_code(code)
        print('验证结果:', rest)
        # 表单是否通过了验证
        if form.is_valid():
            # 执行登录
            # 这个是form的验证后返回的数据
            data = form.cleaned_data

            # 使用自定义的方式实现登录
            # 查询用户信息,form验证后的数据提取username和password
            # 在数据库中取出这个用户
            # user = User.objects.get(username=data['username'], password=data['password'])
            # 设置用户ID保存到session
            # request.session['user_id'] = user.id
            # 登录后的跳转,跳转到首页
            # return redirect('index')

            # 使用django-auth来实现登录,这些都是django-auth提供的登录的方法更简单
            user = authenticate(request,
                                username=data['username'],
                                password=data['password'])
            if user is not None:
                # login
                login(request, user)
                request.session['user_id'] = user.id
                # 登录后的跳转
                # 在开头定义这个next_url并且有解释
                return redirect(next_url)
        else:
            # 打印异常
            print(form.errors)
    else:
        form = UserLoginForm(request)
    return render(
        request,
        'login.html',
        {
            'form': form,
            # 把next_url带到模板中
            'next_url': next_url
        })
Ejemplo n.º 7
0
 def clean_txtCode(self):
     txtCode = self.cleaned_data.get('txtCode')
     if txtCode:
         verify = VerifyCode(self.request)
         if not verify.validate_code(txtCode):
             raise forms.ValidationError('验证码不正确')
     else:
         raise forms.ValidationError('验证码不能为空')
     return txtCode
Ejemplo n.º 8
0
 def clean_verify_code(self):
     """ Verify that the verification code entered by the user is correct"""
     verify_code = self.cleaned_data['verify_code']
     if not verify_code:
         raise forms.ValidationError('please enter verification code')
     client = VerifyCode(self.request)
     if not client.validate_code(verify_code):
         raise forms.ValidationError('your verification code is not correct')
     return verify_code
Ejemplo n.º 9
0
 def clean_verify_code(self):
     """验证用户输入的验证码是否正确"""
     verify_code = self.cleaned_data['verify_code']
     if not verify_code:
         raise forms.ValidationError('请输入验证码')
     client = VerifyCode(self.request)
     if not client.validate_code(verify_code):
         raise forms.ValidationError('您输入的验证码不正确')
     return verify_code
Ejemplo n.º 10
0
    def clean_verify_code(self):
        """检验验证码是否正确"""
        verify_code = self.cleaned_data["verify_code"]
        if not verify_code:
            raise forms.ValidationError("请输入验证码")
        client = VerifyCode(self.request)
        if not client.validate_code(verify_code):
            raise forms.ValidationError("您输入的验证码不正确")

        return verify_code
Ejemplo n.º 11
0
 def clean_verify_code(self):
     """验证用户输入的验证码是否正确"""
     # cleaned_data返回验证后的表单数据
     verify_code = self.cleaned_data['verify_code']
     if not verify_code:
         raise forms.ValidationError('请输入验证码')
     # 这里需要传一个request,那就直接在上面重写一下__init__构造函数,把request传进来
     # 那么在实例化form对象时需要传request
     client = VerifyCode(self.request)
     if not client.validate_code(verify_code):
         raise forms.ValidationError('您输入的验证码不正确')
     return verify_code
Ejemplo n.º 12
0
 def post(self, request):
     print(request.POST)
     code = request.POST.get('txtCode')
     v_code = VerifyCode(request)
     if not v_code.validate_code(code):
         data = {
             'msg': '验证码错啦',
             'status': 0,
         }
         return JsonResponse(data)
     form = LinkForm(request.POST)
     if form.is_valid():
         print(form.cleaned_data)
         txtTitle = form.cleaned_data.get('txtTitle')
         txtUserName = form.cleaned_data.get('txtUserName')
         txtUserTel = form.cleaned_data.get('txtUserTel')
         txtEmail = form.cleaned_data.get('txtEmail')
         txtSiteUrl = form.cleaned_data.get('txtSiteUrl')
         txaArticle = form.cleaned_data.get('txaArticle')
         obj = LinkModel()
         obj.web_name = txtTitle
         obj.contact_man = txtUserName
         obj.phone = txtUserTel
         obj.email = txtEmail
         obj.web_link = txtSiteUrl
         obj.web_description = txaArticle
         obj.save()
         data = {
             'msg': '申请成功',
             'status': 1,
         }
         return JsonResponse(data)
     else:
         print(form.errors)
         error_list = []
         for i in [
                 'txtTitle', 'txtUserName', 'txtUserTel', 'txtEmail',
                 'txtSiteUrl', 'txaArticle'
         ]:
             error = form.errors.get(i)
             if error:
                 error_list.append(error[0])
         if len(error_list) <= 0:
             data = {
                 'msg': '申请失败,请重试',
                 'status': 0,
             }
             return JsonResponse(data)
         data = {
             'msg': error_list[0],
             'status': 0,
         }
         return JsonResponse(data)
Ejemplo n.º 13
0
 def clean_verify_code(self):
     """验证用户输入的验证码是否正确"""
     verify_code = self.cleaned_data['verify_code']
     if not verify_code:
         raise forms.ValidationError('请输入验证码')
     client = VerifyCode(self.request)
     # if只有true的时候才会执行下面的情况if True就是if not False
     # 也就是说client.validate_code(verify_code)是False时
     # 当验证码不正确时,返回False。
     if not client.validate_code(verify_code):
         raise forms.ValidationError('您输入的验证码不正确')
     return verify_code
Ejemplo n.º 14
0
def user_login(request):
    """用户登录"""
    # 第一次访问URL展示表单,GET供用户输入
    # 如果登陆是从其他页面跳转过来的,会带next参数,如果有next参数,登录完成后,需要跳转到next
    # 所对应的地址,否则调到首页上去
    next_url = request.GET.get('next', 'index')
    # 第一次访问URL展示表单,POST
    if request.method == 'POST':
        form = UserLoginForm(request=request, data=request.POST)
        client = VerifyCode(request)  # 调用验证码
        code = request.POST.get('verify_code', None)  # 前台获取的vcode值
        print('验证码:', code)
        rest = client.validate_code(code)  # 验证结果
        print("验证结果:", rest)
        # print(request.POST)
        # 验证是否通过验证
        if form.is_valid():
            #   执行登录
            print('验证通过')
            # 返回验证后的表单数据
            data = form.cleaned_data
            # # 查询用户名信息
            # user = User.objects.get(username=data['username'],password=data['password'])
            # request.session[constants.LOGIN_SESSION_ID] = user.id      # 查询结果设置到session中去
            #
            # return redirect('index')         # 登录后调转到首页
            """使用django-auth来实现登录"""
            user = authenticate(request,
                                username=data['username'],
                                password=data['password'])
            if user is not None:
                # 在视图中获取当前用户
                login(request, user)
                # 登录后的跳转
                print('next_url:', next_url)
                return redirect(next_url)
        else:
            print(form.errors)

    else:
        # get 请求,展示我们的页面
        form = UserLoginForm(request)
    return render(request, 'login.html', {'form': form, 'next_url': next_url})
Ejemplo n.º 15
0
def user_login(request):
    """用户登录"""
    # 如果登录是从其他页面跳转过来的,会带next参数,如果有next参数,登录完成后,
    # 需要跳转到next所对应的地址,否则,跳转到首页上去
    next_url = request.GET.get('next', 'index')
    # 第一次访问url get展示表单,供用户输入
    # 第二次访问url post
    if request.method == 'POST':
        form = UserLoginForm(request=request, data=request.POST)
        client = VerifyCode(request)
        code = request.POST.get('vcode', None)
        rest = client.validate_code(code)
        # 表单是否通过了验证
        if form.is_valid():
            # 执行登录
            data = form.cleaned_data
            # # 查询用户信息
            # user = User.objects.get(username=data['username'], password=data['password'])
            # # 设置用户ID到session
            # request.session[constants.LOGIN_SESSION_ID] = user.id
            # # 登录后的跳转
            # return redirect('index')

            # 使用自定义方法进行登录
            # 使用django-auth来实现登录
            user = authenticate(request,
                                username=data['username'],
                                password=data['password'])
            if user is not None:
                login(request, user)
                request.session['user_id'] = user.id
                # 登录后的跳转

                return redirect(next_url)
        else:
            print(form.errors)
    else:
        form = UserLoginForm(request)
    return render(request, 'user_login.html', {
        'form': form,
        'next_url': next_url
    })
Ejemplo n.º 16
0
def user_login(request):
    """ User login """
    # If the login is jumped from another page, it will bring next parameter, if there is next parameter, after the login is completed, you need to transfer to
    # The address corresponding to next, otherwise, jump to the home page
    next_url = request.GET.get('next', 'index')
    # First visit URL GET display form for user input
    # Second access to URL POST
    if request.method == 'POST':
        form = UserLoginForm(request=request, data=request.POST)
        print(request.POST)
        client = VerifyCode(request)
        code = request.POST.get('vcode', None)
        rest = client.validate_code(code)
        print('Validation results:', rest)
        # Whether the form is verified
        if form.is_valid():
            # Perform login
            data = form.cleaned_data

            # ## Use a custom way to log in
            # # Query user information [MD5] encryption algorithm, irreversible encryption algorithm 1243 -> sdfadfad
            # user = User.objects.get(username=data['username'], password=data['password'])
            # # Set user ID to session
            # request.session[constants.LOGIN_SESSION_ID] = user.id
            # #Jump after login
            # return redirect('index')

            ### Use django-auth to log in
            user = authenticate(request,
                                username=data['username'],
                                password=data['password'])
            if user is not None:
                login(request, user)
                # Jump after login
                return redirect(next_url)
        else:
            print(form.errors)
    else:
        form = UserLoginForm(request)
    return render(request, 'login.html', {'form': form, 'next_url': next_url})
Ejemplo n.º 17
0
def user_login(request):
    """ 用户登录 """
    # 如果登陆是从其他页面跳转过来的,会带一个next参数,
    # 如果有next参数,登陆完之后跳转到之前的地址去,否则到首页
    next_url = request.GET.get('next', 'index')
    # 第一次访问URL GET 展示表单,供用户输入
    # 第二次访问URL POST
    if request.method == 'POST':
        form = UserLoginForm(request=request, data=request.POST)
        print(request.POST)
        client = VerifyCode(request)
        code = request.POST.get('varify_code', None)
        rest = client.validate_code(code)
        print('验证结果: ', rest)
        # 表单是否通过了验证
        if form.is_valid():
            data = form.cleaned_data

            # user = User.objects.get(username=data['username'], password=data['password'])
            # request.session[constants.LOGIN_SESSION_ID] = user.id
            # return redirect('index')

            # 使用django-auth实现登陆
            user = authenticate(request,
                                username=data['username'],
                                password=data['password'])
            if user is not None:
                # 登陆
                login(request, user)
                # 登陆后的跳转
                return redirect(next_url)
        else:
            print(form.errors)
    else:
        form = UserLoginForm(request)
    return render(request, 'login.html', {
        'form': form,
        'next_url': next_url,
    })
Ejemplo n.º 18
0
def user_login(request):
    next_url = request.GET.get('next', 'index')
    if request.method == 'POST':
        form = UserLoginForm(request=request, data=request.POST)
        code = request.POST.get('verify_code', None)
        client = VerifyCode(request)
        rest = client.validate_code(code)
        if form.is_valid():
            data = form.cleaned_data
            # user = User.objects.get(username=data['username'], password=data['password'])
            # request.session[constants.LOGIN_SESSION_ID] = user.id
            # return redirect('index')
            user = authenticate(request,
                                username=data['username'],
                                password=data['password'])
            if user is not None:
                login(request, user)
                request.session['user_id'] = user.id
                return redirect(next_url)
        else:
            print(form.errors)
    else:
        form = UserLoginForm(request)
    return render(request, 'login.html', {'form': form, 'next_url': next_url})
Ejemplo n.º 19
0
def user_login(request):
    """ 用户登录 """
    # 如果登陆是从其他页面跳转过来的,会带 next参数,如果有 next参数,登陆完成后,需要跳转到 next所对应的地址,否则跳转到首页上去
    next_url = request.GET.get('next', 'index')
    # 第一次访问 URL GET 展示表单,供用户输入
    # 第二次访问 URL,POST请求,对用户提交的数据进行操作
    if request.method == 'POST':
        form = UserLoginForm(request=request, data=request.POST)
        print(request.POST)
        client = VerifyCode(request)
        code = request.POST.get('vcode', None)
        result = client.validate_code(code)
        print('验证结果:', result)
        # 表单是否通过验证
        if form.is_valid():
            data = form.cleaned_data
            # user = User.objects.get(username=data['username'], password=data['password'])
            # request.session[constants.LOGIN_SESSION_ID] = user.id
            # return redirect('index')

            # 使用django-auth来实现登陆
            user = authenticate(request, username=data['username'], password=data['password'])
            if user is not None:
                login(request, user)
                request.session['user_id'] = user.id
                # 登陆后的跳转
                return redirect(next_url)
        else:
            print(form.errors)
    # 第一次访问 URL,GRT请求,展示表单,供用户输入
    else:
        form = UserLoginForm(request)
    return render(request, 'login.html', {
        'form': form,
        'next_url': next_url,
    })
Ejemplo n.º 20
0
    def post(self, request):
        # 默认提交失败
        data = {
            'msg': '评论提交失败,请重试',
            'status': 0,
        }
        print(request.POST)  #打印看一下post提交过来的内容
        txtCode = request.POST.get('txtCode')  #提取出验证码
        v_code = VerifyCode(request)  #验证验证码
        if not v_code.validate_code(txtCode):  #如果验证码验证不通过,则报错返回前端
            data = {
                'msg': '验证码错啦',
                'status': 0,
            }
            return JsonResponse(data)

        # if art_uid:  #如果uid对应的文章存在
        u_name = request.POST.get('u_name')  #获取评论用户的名称
        txtContent = request.POST.get('txtContent')  #获取评论内容
        u_code = request.POST.get('u_code')  #获取一级评论的uid
        u_recode = request.POST.get('u_recode')  #获取子评论的uid
        if u_recode:  #判断此提交的评论是否为二级评论,如果是则存进数据表
            cmt = get_object_or_404(ReCommentModel,
                                    u_code=u_recode)  #获取子评论的回复对象
            re_cmt = ReCommentModel()  #获取子评论表的对象
            re_cmt.self_reply = cmt  #外键关联要回复的子评论对象
            re_cmt.to_code = u_code  #保存回复的一级评论的uid,定位自己在哪一个一级评论下
            re_cmt.to_recode = u_recode  #保存回复的二级评论的uid,这里的uid存的是子评论表中的评论的uid
            re_cmt.txtContent = txtContent  #保存回复内容
            re_cmt.u_name = u_name  #保存回复者的名称
            re_cmt.save()  #这里保存的是回复子评论的内容
            data = {
                'msg': '评论提交成功',
                'status': 1,
            }
            return JsonResponse(data)

        if u_code:
            cmt = get_object_or_404(CommentModel, u_code=u_code)  #外键关联一级评论对象
            re_cmt = ReCommentModel()
            re_cmt.comment = cmt  #保存一级评论对象
            re_cmt.to_code = u_code  #保存一级评论的uid
            re_cmt.txtContent = txtContent  #保存回复内容
            re_cmt.u_name = u_name  #保存回复者名字
            re_cmt.save()  #这里保存的是回复一级评论的内容
            data = {
                'msg': '评论提交成功',
                'status': 1,
            }
            return JsonResponse(data)

        art_uid = request.POST.get('art_uid')  # 取评论所在文章的uid,保证评论与文章相关联
        note_id = request.POST.get('note_id')
        photo_id = request.POST.get('photo_id')
        mall_uid = request.POST.get('mall_uid')
        comment = CommentModel()
        if art_uid:
            self.help_save(request, comment, u_name, txtContent)
            comment.art_uid = art_uid  #保存文章uid
            comment.save()  #这里保存的是一级评论的内容
        elif note_id:
            self.help_save(request, comment, u_name, txtContent)
            comment.note_id = note_id
            comment.save()
        elif photo_id:
            self.help_save(request, comment, u_name, txtContent)
            comment.photo_id = photo_id
            comment.save()
        elif mall_uid:
            self.help_save(request, comment, u_name, txtContent)
            comment.mall_uid = mall_uid
            comment.save()
        data = {
            'msg': '评论提交成功',
            'status': 1,
        }
        return JsonResponse(data)