0

I am trying to implement Django Recaptcha using this package: https://github.com/praekelt/django-recaptcha

This is my 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.')})

According to the documentation or as I saw it, this is all that is required. But the post request for the form doesn't work. I get this message under my server logs:

UnboundLocalError: local variable 'redirect_url' referenced before assignment

The error message refers back to my views.py code (redirect_url = LOGIN_URL) which is below:

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)

I should add I have added my recapctcha keys to settings.py

Any help is really appreciated.

UPDATED: Full Error Message as requested:

> 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
Rutnet
  • 1,533
  • 5
  • 26
  • 48
  • Please post the full stack trace for the error. The code you've posted wouldn't result in this error, which suggests it's either coming from somewhere else, or you've not copied exactly the code you have locally. – solarissmoke Jan 23 '20 at 04:45
  • Sorry for the delay, just updated – Rutnet Jan 24 '20 at 22:02
  • The last line in the stack trace refers to a line of code that reads: `return JsonResponse({'redirect_url': redirect_url, "status": True})`. That line of code is where the problem is, but it's missing in the code you've shared above. – solarissmoke Jan 25 '20 at 01:29

0 Answers0