尝试使Check reCaptcha Mixin用于登录视图-Django

时间:2018-07-06 08:21:56

标签: python django django-forms django-views recaptcha

我正在尝试进行混合,以检查有效的reCaptcha。 我是django的新手,我试图用我所知道的来做

它正在正确检查,但权限存在问题。 当它立即再次将我重定向到登录页面时,它将获得身份验证并重定向到仪表板,即成功页面。

看看mixin。

class CheckRecaptchaMixin(AccessMixin):

    error_message = 'Invalid reCAPTCHA. Please try again.'

    def get_error_message(self):
        return self.error_message

    def handle_not_valid_recaptcha(self):

        message = self.get_error_message()
        if self.raise_exception:
            raise PermissionDenied(message)
        messages.error(self.request, message)
        print('login redirect')
        return redirect_to_login(self.request.get_full_path(), self.get_login_url(), self.get_redirect_field_name())


    def dispatch(self,request, *args, **kwargs):

        request.recaptcha_is_valid = None
        if request.method == 'POST':
            recaptcha_response = request.POST.get('g-recaptcha-response')
            data = {
                'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY,
                'response': recaptcha_response
            }
            r = requests.post('https://www.google.com/recaptcha/api/siteverify', data=data)
            result = r.json()
            if result['success']:
                print('This is success')
                request.recaptcha_is_valid = True
            else:
                print('This is error')
                request.recaptcha_is_valid = False
                self.handle_not_valid_recaptcha()

        return super(CheckRecaptchaMixin, self).dispatch(request,*args, **kwargs)

在我的views.py

class UserLoginView(CheckRecaptchaMixin,LoginView):
   template_name = 'accounts/login.html'
   redirect_field_name='next'

当reCaptcha正确时,仅打印This is success;当reCaptcha不正确时,则打印This is errorlogin redirect

这意味着它正在成功处理reCaptcha,但没有重定向到登录页面,需要注意的另一件事是,在我的注销页面上,我看到error_message是我在mixin中定义的('无效的reCAPTCHA。请重试。 ')

1 个答案:

答案 0 :(得分:0)

我没有通过身份验证的方法是通过get_form方法在表单中添加错误,这里是有效的mixin。

class CheckRecaptchaMixin(AccessMixin):

    error_message = 'Invalid reCAPTCHA. Please try again.'
    not_activated_redirect = ''
    def get_error_message(self):
        return self.error_message

    def dispatch(self,request, *args, **kwargs):

        request.recaptcha_is_valid = None
        if request.method == 'POST':
           recaptcha_response = request.POST.get('g-recaptcha-response')
            data = {
                'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY,
                'response': recaptcha_response
            }
            r = requests.post('https://www.google.com/recaptcha/api/siteverify', data=data)
            result = r.json()
            if result['success']:
                request.recaptcha_is_valid = True
            else:
                request.recaptcha_is_valid = False

        return super(CheckRecaptchaMixin, self).dispatch(request,*args, **kwargs)

    def get_form(self,form_class=None):
        form = super().get_form(form_class)
        if self.request.method == 'POST' and form.is_valid():
            if not self.request.recaptcha_is_valid:
                form.add_error(None,self.get_error_message())
        return form

如果有更好的选择,谢谢,他/她可以回答我。