0

My form class

from django import forms

class Form(forms.Form):
    your_name = forms.CharField(label='Your name ', max_length=100)

My app file __init__.py

from django import forms
from my_app.forms import Form
from captcha.fields import ReCaptchaField

def register(form):
 form.captcha=CaptchaField()

register(Form)

Code in __init__.py add an attribute captcha but it is not on the page.

I tried so

My form class

from django import forms

class Form(forms.Form):
    your_name = forms.CharField(label='Your name ', max_length=100)
    captcha=CaptchaField()

It works, but I have a different! I want to add a сaptcha many forms. I want to add the captcha without changing the form classes. I want to change the form classes during initialization. How to do it???

2
  • Would a superclass (subclass of django.forms.Form) with the captcha attribute solve your problem? Commented Jun 15, 2015 at 8:14
  • Please read more. I do not understand what you mean. Commented Jun 15, 2015 at 9:20

2 Answers 2

1

You need to assign the field to form.fields, not just form.

However this is not really the way to do this. Instead, you should make all your forms inherit from a shared parent class, which defines the captcha field.

Sign up to request clarification or add additional context in comments.

1 Comment

It works MyForm.base_fields['foo'] = CharField(max_length=100)
0

Continuing from a comment, have a base class inherit from django.forms.Form, and subclass it for all the forms you want a captcha field on;

from captcha.fields import ReCaptchaField
from django.forms import Form

class CaptchaMixin():
    captcha = ReCaptchaField()

class ActualForm(CaptchaMixin, Form):
    pass

1 Comment

Thank you! But is there a way to add a class forms attribute in __init__py?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.