0

I'm trying to add captcha field to Django UserCreationFrom and I can not solve it. I can not post everything what I tried because I'm trying modify this view about 2 hours. I have register form on my site made by Django UserCreationFrom - everything is ok but I decied to add Recaptcha. I have app name "accounts" and in this app I have view in which is this:

from django.contrib.auth.forms import UserCreationForm, get_user_model
from django.template.context_processors import request
from django.urls import reverse_lazy
from django.views import generic
from captcha.fields import ReCaptchaField
from captcha.widgets import ReCaptchaV2Checkbox

class SignUpForm(UserCreationForm):
    class Meta:
        model = get_user_model()
        fields = ('first_name', "last_name",
                  "username", "email", "password1", "password2")
    class Captcha(UserCreationForm):
        captcha = ReCaptchaField(widget=ReCaptchaV2Checkbox)


class SignUpView(generic.CreateView):
    form_class = SignUpForm
    success_url = reverse_lazy("login")
    template_name = 'accounts/signup.html'

Signup.html looks like this:

{% extends 'main.html' %}

{% block title %} Přihlášení {% endblock %}

{% block content %}

    <h2>Registrace</h2>
    <div class="box">
        <form method="post">
            {% csrf_token %}
            {{ form.as_p }}
            
            <button type="submit"> Registrovat </button>

        </form>

    </div>

{% endblock %}

I installed the django-recaptcha library - added it in settings. Also I have in settings my RECAPtCHA_PUBLIC and PRIVATE KEY and I'm registered in Google_recaptcha website.

My URLS looks like this:

from django.contrib import admin
from django.urls import path, include
import claim.views
import contact.views
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = [   
    # accounts
    path("accounts/", include("accounts.urls")),
    path("accounts/", include("django.contrib.auth.urls")),

As I said - the registration form works but no Captcha here:

Registration form

But I cannot find how to modify UserCreationForm to add recaptcha. I found many ways how to add Captcha to your own form but no advice how to add in to UserCreaiton form. I also read this: Implement Django Simple Captcha with the existing django.contrib.auth.forms but it doesnt work for me.

I'm new to python, new to django and I'm creating my first full stack work.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Balage
  • 25
  • 8
  • Do you have default `User` model? – Sunderam Dubey Oct 07 '22 at 12:24
  • I have some models but not in "account" app but in another app named "claim"class Users(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) f_name = models.CharField(max_length=200, blank=False) l_name = models.CharField(max_length=200) address = models.CharField(max_length=200) email = models.EmailField(max_length=200, blank=False) phone = models.CharField(max_length=200) customer_has_claim = models.ManyToManyField(Claim) – Balage Oct 07 '22 at 12:31

0 Answers0