html <form id="add_form_order" method="post"> {% csrf_token %} <div class="form-group">{{form.choice_services}}</div> <div class="form-group">{{form.name}}</div> <div class="form-group">{{form.telephone_number}}</div> <div class="form-group">{{form.email}}</div> <div class="form-group">{{form.message}}</div> <div class="form-group">{{form.contact_text}}</div> {{ form.captcha }} </form>
<!--recaptcha--> <script src="https://www.google.com/recaptcha/api.js?render=reCAPTCHA_site_key"></script> <script> window.onload = function() { var $recaptcha = document.querySelector('#g-recaptcha-response'); if($recaptcha) { $recaptcha.setAttribute("required", "required"); } const $form = document.querySelector('form'); $form.addEventListener('submit', (event) => { event.preventDefault(); console.log('prevented submit by code for demo'); }); }; </script> css #g-recaptcha-response { display: block !important; position: absolute; margin: -78px 0 0 0 !important; width: 302px !important; height: 76px !important; z-index: -999999; opacity: 0; } пятница, 12 марта 2021 г. в 21:06:56 UTC+1, RyuCoder: > Remove below line and try again > > self.fields['captcha'].widget.attrs['required'] = 'True' > > > If the problem persists, try some js or jquery validation in frontend > > > > > > On Thu, Mar 11, 2021, 9:19 PM Gabriel Araya Garcia <gabriela...@gmail.com> > wrote: > >> Camarada Sokov: >> Your code is very difficult to understand at first sight. I did not know >> about ReCaptchaField(). >> But, is interesting to learn something new. >> >> Recards, >> >> Gabriel Araya Garcia >> GMI - Desarrollo de Sistemas Informáticos >> from Santiago de Chile >> >> >> >> El jue, 11 mar 2021 a las 12:40, Sergei Sokov (<soko...@gmail.com>) >> escribió: >> >>> I put the google recaptcha V2 to my django project form. It looks like >>> working, but if I leave empty the recaptcha checkbox my form is sent still. >>> >>> forms.py >>> from django import >>> forms from captcha.fields >>> import ReCaptchaField >>> from django.forms import Textarea >>> from .models import * >>> class CustomerForm(forms.ModelForm): >>> captcha = ReCaptchaField( public_key='key', private_key='key', ) >>> class Meta: >>> model = Customer fields = '__all__' >>> def __init__(self, *args, **kwargs): >>> # get 'user' param from kwargs >>> user = kwargs.pop('user', None) >>> super().__init__(*args, **kwargs) >>> self.fields['message'].widget = Textarea(attrs={'rows': 4}) >>> self.fields['i_have_read_rules'].widget.attrs['required'] = >>> 'True' >>> self.fields['i_agree'].widget.attrs['required'] = 'True' >>> self.fields['captcha'].widget.attrs['required'] = 'True' >>> for field in self.fields: >>> self.fields[field].widget.attrs['class'] = 'form-control' >>> self.fields['i_have_read_rules'].widget.attrs['class'] = >>> 'form-check' >>> self.fields['i_agree'].widget.attrs['class'] = 'form-check' >>> >>> html >>> <form id="add_form_order" method="post"> >>> {% csrf_token %} >>> <h6 class="block-title">Выберите услугу</h6> >>> <div >>> class="form-group">{{form.choice_services}}</div> >>> <div class="form-group">{{form.name}}</div> >>> <div >>> class="form-group">{{form.telephone_number}}</div> >>> <div class="form-group">{{form.email}}</div> >>> <div class="form-group">{{form.message}}</div> >>> <div class="form-group">{{form.contact_text}}</div> >>> {{ >>> form.captcha }} >>> </form> >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Django users" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to django-users...@googlegroups.com. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/django-users/e18dcba2-07e2-44d9-ace6-744a2bb69234n%40googlegroups.com >>> >>> <https://groups.google.com/d/msgid/django-users/e18dcba2-07e2-44d9-ace6-744a2bb69234n%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-users...@googlegroups.com. >> > To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/CAKVvSDD_HZmL%2BUVgsFVZgQCuyuhCs3Ofe2DkOtne-ydp%2B3joyw%40mail.gmail.com >> >> <https://groups.google.com/d/msgid/django-users/CAKVvSDD_HZmL%2BUVgsFVZgQCuyuhCs3Ofe2DkOtne-ydp%2B3joyw%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f3ec25b0-c6b2-4559-9402-566e11ace3c0n%40googlegroups.com.