0

我正在尝试使用这个包实现 Django Recaptcha:https ://github.com/praekelt/django-recaptcha

这是我的forms.py

class SignupForm(forms.ModelForm):
    captcha = ReCaptchaField(widget=ReCaptchaV2Invisible)
    name = forms.CharField(widget=forms.TextInput)
    lastname = forms.CharField(widget=forms.TextInput)
    password = forms.CharField(
        widget=forms.PasswordInput)

    confirmpassword = forms.CharField(
        widget=forms.PasswordInput
    )
    email = forms.EmailField(
        error_messages={
            'unique': pgettext_lazy(
                'Registration error',
                'This email has already been registered.')})

根据文档或我所见,这就是所需要的。但是表单的发布请求不起作用。我在我的服务器日志下收到此消息:

UnboundLocalError:分配前引用的局部变量“redirect_url”

错误消息引用了我的views.py代码(redirect_url = LOGIN_URL),如下所示:

def signup(request):
    form = SignupForm(request.POST or None)
    if form.is_valid():
        form.save(request)
        if settings.EMAIL_VERIFICATION_REQUIRED:
            msg = 'User has been created. Check your e-mail to verify your e-mail address.'
            messages.success(request, msg)
            redirect_url = LOGIN_URL
        else:
            password = form.cleaned_data.get('password')
            email = form.cleaned_data.get('email')
            user = auth.authenticate(
                request=request, email=email, password=password)
            if user:
                auth.login(request, user)
            messages.success(request, _('User has been created'))
            redirect_url = request.POST.get('next', settings.LOGIN_REDIRECT_URL)
        return redirect(redirect_url)
    ctx = {'form': form}
    return TemplateResponse(request, 'account/signup.html', ctx)

我应该添加我已将我的 recapctcha 键添加到 settings.py

非常感谢任何帮助。

更新:根据要求的完整错误消息:

> ERROR django.request Internal Server Error: /signup/
> [PID:33:MainThread] Traceback (most recent call last):   File
> "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py",
> line 35, in inner
>     response = get_response(request)   File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py",
> line 128, in _get_response
>     response = self.process_exception_by_middleware(e, request)   File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py",
> line 126, in _get_response
>     response = wrapped_callback(request, *callback_args, **callback_kwargs)   File "/usr/local/lib/python3.6/dist-packages/django/views/decorators/csrf.py",
> line 54, in wrapped_view
>     return view_func(*args, **kwargs)   File "/var/www/html/applications/py/saleor/cart/utils.py",
> line 103, in func
>     response = view(request, *args, **kwargs)   File "/var/www/html/applications/py/saleor/core/views.py",
> line 51, in signup
>     return JsonResponse({'redirect_url': redirect_url, "status": True}) UnboundLocalError: local variable 'redirect_url' referenced
> before assignment
4

0 回答 0