Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Captcha allows invalid input #181

Open
kkmehta03 opened this issue Apr 23, 2020 · 1 comment
Open

Captcha allows invalid input #181

kkmehta03 opened this issue Apr 23, 2020 · 1 comment

Comments

@kkmehta03
Copy link

I added the captcha field in accordance with the instructions here. And the captcha field shows up in the custom form.
However, it allows me to the next url even if I have entered the wrong input to the captcha.
In the sense, it is not validating the captcha correctly. Can someone help me out? What am I doing wrong?
Here's the form :

class SomeForm(AuthenticationForm):
    business = forms.ModelChoiceField(
        queryset=Business.objects.all().order_by('bus_name'),
        required=True,
        empty_label="Select Business")

    phone = forms.IntegerField(
        label=" ",
        widget=forms.TextInput(
            attrs={
                'class': 'form-control',
                'style': 'width:300px',
                'name': 'phone',
                "min": "6666666666",
                "max": "9999999999",
                'placeholder': "Mobile number"
            }))
    password = forms.CharField(
        label=" ",
        max_length=30,
        widget=forms.PasswordInput(
            attrs={
                'class': 'form-control',
                'name': 'phone',
                'style': 'width:300px',
                'password': forms.PasswordInput(),
                'placeholder': "Enter your password"
            }))
    captcha = CaptchaField()

Here's the views.py :

def login(request):
    form = SomeForm()
    if request.method == 'POST':
        captcha_form = SomeForm(request, request.POST)
        if captcha_form.is_valid:
           url = reverse('dashboard')
           return HttpResponse(url)
       else:
           print('not valid form')

Here's the input form on clicking submit :
request.POST:

<QueryDict: {'csrfmiddlewaretoken': ['some_token'], 'phone': ['999999999'], 'password': ['pass'], 'business': ['1'], 'captcha_0': ['f44cda7f7d14bfbaa76ba702cfa477a99b3d4160'], 'captcha_1': ['j']}>

the captcha form:

<tr><th><label for="id_phone"> :</label></th><td><input type="text" name="phone" value="9999999999" class="form-control" style="width:300px" name="phone" min="6666666666" max="9999999999" placeholder="Mobile number" required id="id_phone"></td></tr>
<tr><th><label for="id_password"> :</label></th><td><input type="password" name="password" class="form-control" name="phone" style="width:300px" password="&lt;django.forms.widgets.PasswordInput object at 0x3fs822310&gt;" placeholder="Enter your password" maxlength="30" required id="id_password"></td></tr>
<tr><th><label for="id_captcha_1">Captcha:</label></th><td><ul class="errorlist"><li>Invalid CAPTCHA</li></ul><img src="/auth/captcha/image/148bf605b02c87516ad088f7e43d5a6c41d04264/" alt="captcha" class="captcha" />
<input type="hidden" name="captcha_0" value="148bf605b02c87516ad088f7e43d5a6c41d04264" required id="id_captcha_0"><input type="text" name="captcha_1" required id="id_captcha_1" autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false"><input type="hidden" name="organization" value="1" id="id_organization"></td></tr>
@Pyvonix
Copy link

Pyvonix commented Nov 26, 2022

Hi @kmehta03,

They are two things in your views.py:

  1. SomeForm should only take the current posted request
  2. is_valid is a method

That should be something like:

def login(request):
    if request.method == 'POST':
        captcha_form = SomeForm(request.POST)
        if captcha_form.is_valid():
           url = reverse('dashboard')
           return HttpResponse(url)
        else:
           print('not valid form')
    else:
        # It's GET 
        form = SomeForm()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants