recaptcha impossible insert in form reset confirm

Hi, impossible imported captcha in form reset confirm …

my code urls.py:

    path('password_reset/', auth_views.PasswordResetView.as_view(form_class=forms.PasswordReset), name='password_reset'),
    path('password_reset_done/', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
    path('reset/confirm/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(form_class=forms.PasswordResetForm),  name='password_reset_confirm'),
    path('reset/complete/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
    

My views.py:

from django.http.response import Http404
from django.views import generic
from django.urls import reverse_lazy
from django.shortcuts import get_object_or_404, render
import braces.views
from . import forms
from . models import User



class RegisterView(generic.CreateView):
    form_class = forms.RegisterForm
    success_url = reverse_lazy('login')
    template_name = "registration/register.html"


class Profile(braces.views.LoginRequiredMixin, generic.DetailView):
    model = User
    def get_object(self):
        return get_object_or_404(User, username=self.kwargs['username'])

class ProfileUpdate(braces.views.LoginRequiredMixin, generic.UpdateView):
    model = User
    def get_success_url(self):
        return reverse_lazy("user_profile", kwargs={"username": self.request.user.username})
    def get_object(self):
        return get_object_or_404(User, username=self.request.user.username)


class ProfileUpdatePhoto(braces.views.LoginRequiredMixin, generic.UpdateView):
    model = User
    success_url = reverse_lazy('user_profile_update_photo')
    def get_success_url(self):
        return reverse_lazy("user_profile", kwargs={"username": self.request.user.username})
    def get_object(self):
        return get_object_or_404(User, username=self.request.user.username)


html:

{% extends "layout/normal.html" %}
{% load crispy_forms_tags %}
{% block SITE_TITLE %} Reset conferma password {% endblock %}
{% block PAGE_TITLE %} Reset conferma password {% endblock %}

{% block content %}

{% if validlink %}
<p>Inserisci e conferma la nuova Password</p>
<form method="POST" novalidate>
    {% csrf_token %}
    <div class="form-row">
        <div class="form-group mb-0">
            {{ form.new_password1|as_crispy_field }}
        </div>
        <div class="form-group mb-0">
            {{ form.new_password2|as_crispy_field }}
        </div>
        <div class="form-group mb-0">
            {{ form.captcha|as_crispy_field }}
        </div>        
        <input type="submit" class="btn btn-info" value="Cambia Password">
</form>
{% else %}
<h1>Errore.</h1>
<p>Il link seguito non è più valido!</p>
{% endif %}
{% endblock %}

How to fixed problem ? using plugin: django-recaptcha · PyPI

Thanks, good night.

Please post the forms you are using. (forms.PasswordReset and forms.PasswordResetForm) Ensure that you have added the captcha field in those forms as described in the docs. (See GitHub - django-recaptcha/django-recaptcha: Django reCAPTCHA form field/widget integration app.)

ok, forms.py:

from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, PasswordChangeForm, PasswordResetForm, SetPasswordForm, UserChangeForm
from django import forms
from django_recaptcha.fields import ReCaptchaField
from django import forms
from . models import User
from django.utils.translation import gettext_lazy as _
from .widgets import CustomDatePickerWidget



class RegisterForm(UserCreationForm):
    captcha = ReCaptchaField()
    class Meta:
        model = User
        fields = ('email', 'username', 'password1', 'password2')


class LoginForm(AuthenticationForm):
    username = forms.CharField(label='Email / Username')
    captcha = ReCaptchaField()


class PasswordChange(PasswordChangeForm):
    captcha = ReCaptchaField()

class PasswordReset(PasswordResetForm):
    captcha = ReCaptchaField()
    
class PasswordResetForm(SetPasswordForm):
    captcha = ReCaptchaField()


class ProfileUpdate(forms.ModelForm):
    GENDER = (
        ("Male", _('Male')),
        ("Female", _('Female')),
        ("n/a", _('n/a')),
    )
    first_name = forms.CharField(label=_('First name'), widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': "Enter your first name"}), required = False)
    last_name = forms.CharField(label=_('Last name'), widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': "Enter your last name"}), required = False)
    gender = forms.ChoiceField(label=_('Gender'), choices=GENDER, widget=forms.RadioSelect, required=False)
    birthday = forms.DateField(label=_('Birthday'), required = False, widget=CustomDatePickerWidget())
    city = forms.CharField(label=_('City'), widget=forms.TextInput(attrs={'class':'form-control'}), required=False)
    location = forms.CharField(label=_('Location'), widget=forms.TextInput(attrs={'class':'form-control'}), required=False)
    phone = forms.CharField(label=_('Phone number'), widget=forms.TextInput(attrs={'class':'form-control'}), required=False)
    info = forms.CharField(label=_('Information user'), widget=forms.Textarea(attrs={"style": "height:160px; width:100%;"}), required=False)
    class Meta:
        model =User
        fields = ['first_name', "last_name",  'gender', 'birthday', 'city', 'location', 'phone', 'info']

class ProfileUpdatePhoto(forms.ModelForm):
    photo = forms.ImageField(required = False)
    class Meta:
        model =User
        fields = [ 'photo']

1 ) Ok, in password reset form

  1. Not working in form reset confirm form

code error:

my html:

{% extends "layout/normal.html" %}
{% load crispy_forms_tags %}
{% block SITE_TITLE %} Reset conferma password {% endblock %}
{% block PAGE_TITLE %} Reset conferma password {% endblock %}

{% block content %}

{% if validlink %}
<p>Inserisci e conferma la nuova Password</p>
<form method="POST" novalidate>
    {% csrf_token %}
    <div class="form-row">
        <div class="form-group mb-0">
            {{ form.new_password1|as_crispy_field }}
        </div>
        <div class="form-group mb-0">
            {{ form.new_password2|as_crispy_field }}
        </div>
        <div class="form-group mb-0">
            {{ form.captcha|as_crispy_field }}
        </div>        
        <input type="submit" class="btn btn-info" value="Cambia Password">
</form>
{% else %}
<h1>Errore.</h1>
<p>Il link seguito non è più valido!</p>
{% endif %}
{% endblock %}

Ok thanks, How to fixed problem ?
Good morning.

error console :

crispy_forms.exceptions.CrispyError: |as_crispy_field got passed an invalid or inexistent field

my change module … using now module : GitHub - alsoicode/django-simple-math-captcha: An easy-to-use math field/widget captcha for Django forms.

my new code:

from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, PasswordChangeForm, PasswordResetForm, SetPasswordForm, UserChangeForm
from django import forms
from simplemathcaptcha.fields import MathCaptchaField
from django import forms
from . models import User
from django.utils.translation import gettext_lazy as _
from core.widgets import CustomDatePickerWidget



class RegisterForm(UserCreationForm):
    captcha = MathCaptchaField()
    class Meta:
        model = User
        fields = ('email', 'username', 'password1', 'password2')


class LoginForm(AuthenticationForm):
    username = forms.CharField(label='Email / Username')
    captcha = MathCaptchaField()


class PasswordChange(PasswordChangeForm):
    captcha = MathCaptchaField()

class PasswordReset(PasswordResetForm):
    captcha = MathCaptchaField()
    
class CaptchaPasswordResetForm(SetPasswordForm):
    captcha = MathCaptchaField()


class ProfileUpdate(forms.ModelForm):
    GENDER = (
        ("Male", _('Male')),
        ("Female", _('Female')),
        ("n/a", _('n/a')),
    )
    first_name = forms.CharField(label=_('First name'), widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': "Enter your first name"}), required = False)
    last_name = forms.CharField(label=_('Last name'), widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': "Enter your last name"}), required = False)
    gender = forms.ChoiceField(label=_('Gender'), choices=GENDER, widget=forms.RadioSelect, required=False)
    birthday = forms.DateField(label=_('Birthday'), required = False, widget=CustomDatePickerWidget())
    city = forms.CharField(label=_('City'), widget=forms.TextInput(attrs={'class':'form-control'}), required=False)
    location = forms.CharField(label=_('Location'), widget=forms.TextInput(attrs={'class':'form-control'}), required=False)
    phone = forms.CharField(label=_('Phone number'), widget=forms.TextInput(attrs={'class':'form-control'}), required=False)
    info = forms.CharField(label=_('Information user'), widget=forms.Textarea(attrs={"style": "height:160px; width:100%;"}), required=False)
    class Meta:
        model =User
        fields = ['first_name', "last_name",  'gender', 'birthday', 'city', 'location', 'phone', 'info']

class ProfileUpdatePhoto(forms.ModelForm):
    photo = forms.ImageField(required = False)
    class Meta:
        model =User
        fields = [ 'photo']

How to impossible integrated captcha in reset confirm form ?

Look at the URL being used for that page (/accounts/reset/<user>/set-password/) - I do not see where you are overriding that url in your urls.py, and so your custom form is not being used on that view.

models.py:

from django.db import models
from django.contrib.auth.models import AbstractUser, PermissionsMixin
from django.utils.translation import gettext_lazy as _
from stdimage import StdImageField
from core.utils import RandomFileName, resize_and_autorotate

class User(AbstractUser, PermissionsMixin):
        GENDER = (
               ("Male", _('Male')),
               ("Female", _('Female')),
               ("n/a", _('n/a')),
        )
        photo    =StdImageField(_('Photo'), upload_to=RandomFileName("photo/"), delete_orphans=True, render_variations=resize_and_autorotate,  blank=True ,  variations={'avatar': (120, 120, True),})
        email    =models.EmailField(_('Email address'), unique=True, error_messages={'unique':_("This email has already been registered.")})
        gender   =models.CharField(_('Gender'), choices=GENDER, default='n/a',max_length=50)
        birthday =models.DateField(_('Birthday'), null=True, blank=True)
        info     =models.TextField(_('Information user'), null=True, blank=True)

        city     =models.CharField(_('City'), null=True, blank=True,max_length=255)
        location =models.CharField(_('Location'), null=True, blank=True,max_length=255)
        phone    =models.CharField(_('Phone number'), null=True, blank=True,max_length=255)


        USERNAME_FIELD  = 'username'
        EMAIL_FIELD     = 'email'
        REQUIRED_FIELDS = ['first_name', 'last_name', 'email']

        def save(self, *args, **kwargs):
             super(User, self).save(*args, **kwargs)

views.py

from django.http.response import Http404
from django.views import generic
from django.urls import reverse_lazy
from django.shortcuts import get_object_or_404, render
import braces.views
from . import forms
from . models import User



class RegisterView(generic.CreateView):
    form_class = forms.RegisterForm
    success_url = reverse_lazy('login')
    template_name = "registration/register.html"


class Profile(braces.views.LoginRequiredMixin, generic.DetailView):
    model = User
    def get_object(self):
        return get_object_or_404(User, username=self.kwargs['username'])

class ProfileUpdate(braces.views.LoginRequiredMixin, generic.UpdateView):
    model = User
    def get_success_url(self):
        return reverse_lazy("user_profile", kwargs={"username": self.request.user.username})
    def get_object(self):
        return get_object_or_404(User, username=self.request.user.username)


class ProfileUpdatePhoto(braces.views.LoginRequiredMixin, generic.UpdateView):
    model = User
    success_url = reverse_lazy('user_profile_update_photo')
    def get_success_url(self):
        return reverse_lazy("user_profile", kwargs={"username": self.request.user.username})
    def get_object(self):
        return get_object_or_404(User, username=self.request.user.username)

forms.py

from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, PasswordChangeForm, PasswordResetForm, SetPasswordForm, UserChangeForm
from django import forms
from simplemathcaptcha.fields import MathCaptchaField
from django import forms
from . models import User
from django.utils.translation import gettext_lazy as _
from core.widgets import CustomDatePickerWidget



class RegisterForm(UserCreationForm):
    captcha = MathCaptchaField()
    class Meta:
        model = User
        fields = ('email', 'username', 'password1', 'password2')


class LoginForm(AuthenticationForm):
    username = forms.CharField(label='Email / Username')
    captcha = MathCaptchaField()


class PasswordChangeForm(PasswordChangeForm):
    captcha = MathCaptchaField()

class PasswordResetForm(PasswordResetForm):
    captcha = MathCaptchaField()
    
class CaptchaPasswordResetForm(SetPasswordForm):
    captcha = MathCaptchaField()

class ProfileUpdate(forms.ModelForm):
    GENDER = (
        ("Male", _('Male')),
        ("Female", _('Female')),
        ("n/a", _('n/a')),
    )
    first_name = forms.CharField(label=_('First name'), widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': "Enter your first name"}), required = False)
    last_name = forms.CharField(label=_('Last name'), widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': "Enter your last name"}), required = False)
    gender = forms.ChoiceField(label=_('Gender'), choices=GENDER, widget=forms.RadioSelect, required=False)
    birthday = forms.DateField(label=_('Birthday'), required = False, widget=CustomDatePickerWidget())
    city = forms.CharField(label=_('City'), widget=forms.TextInput(attrs={'class':'form-control'}), required=False)
    location = forms.CharField(label=_('Location'), widget=forms.TextInput(attrs={'class':'form-control'}), required=False)
    phone = forms.CharField(label=_('Phone number'), widget=forms.TextInput(attrs={'class':'form-control'}), required=False)
    info = forms.CharField(label=_('Information user'), widget=forms.Textarea(attrs={"style": "height:160px; width:100%;"}), required=False)
    class Meta:
        model =User
        fields = ['first_name', "last_name",  'gender', 'birthday', 'city', 'location', 'phone', 'info']

class ProfileUpdatePhoto(forms.ModelForm):
    photo = forms.ImageField(required = False)
    class Meta:
        model =User
        fields = [ 'photo']

urls.py

from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
from . import forms

urlpatterns = [

    path('login/', auth_views.LoginView.as_view(form_class=forms.LoginForm), name='login'),
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),
    path('register/', views.RegisterView.as_view(), name="register"),


    path('password_reset/', auth_views.PasswordResetView.as_view(form_class=forms.PasswordResetForm), name='password_reset'),
    path('password_reset_done/', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
    path('reset/confirm/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(form_class=forms.CaptchaPasswordResetForm),  name='password_reset_confirm'),
    path('reset/complete/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
    
    path('password_change/', auth_views.PasswordChangeView.as_view(), name='password_change'),
    path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(), name='password_change_done'),

    path('profile/update/', views.ProfileUpdate.as_view(template_name="user_profile_update.html", form_class=forms.ProfileUpdate), name='user_profile_update'),
    path('profile/update/photo/', views.ProfileUpdatePhoto.as_view(template_name="user_profile_update_photo.html", form_class=forms.ProfileUpdatePhoto), name='user_profile_update_photo'),
    path('profile/<str:username>/', views.Profile.as_view(template_name="user_profile.html"), name='user_profile'),
]

reduce html in form|crispy

{% load i18n %}

{% load crispy_forms_tags %}

{% block SITE_TITLE %} Reset Conferma Password {% endblock %}

{% block PAGE_TITLE %} Reset Conferma Password {% endblock %}

{% block content %}

{% if validlink %}
<p>Please enter (and confirm) your new password.</p>
<form action="" method="post" novalidate>
    {% csrf_token %}
    {{ form|crispy}}
   <input type="submit" value="Change my password" />
</form>
{% else %}
<h1>Password reset failed</h1>
<p>The password reset link was invalid, possibly because it has already been used. Please request a new password reset.</p>
{% endif %}
{% endblock %}

ok, that’s all I did with the “accounts” app.
I don’t know how to make sure that when they confirm the password there should be a mathematical block to solve and then the password is confirmed. this is all I have.

thank you very much and have a good evening.

fixed, solved problem

my remove : path(“accounts/”, include(“django.contrib.auth.urls”)),

solved problem

thanks.