Reputation: 1673
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
Upvotes: 0
Views: 173