如何让验证码有一个bootstrap class?

how to make the captcha have a bootstrap class?

我的验证码有问题,我正在使用“Django 简单验证码”,问题是它不允许我放置 bootstrap class 以便输入更好看。

我试过:

html

<label class="form-label">Captcha</label>
{% render_field form.captcha class="form-control" %}

forms.py

class RegisterForm(UserCreationForm):
   captcha=CaptchaField(widget=forms.TextInput(attrs={'class': 'form-control'}))

style.css

#id_captcha_1{
   height: 34px;
   padding: 6px 12px;
   font-size: 14px;
   line-height: 1.42857143;
   color: #555;
   background-color: #fff;
   background-image: none;
   border: 1px solid #ccc;
   border-radius: 4px;
   }

有什么想法可以使输入具有 bootstrap class?

我还没有验证过,但你可以试试:

from captcha.fields import CaptchaField, CaptchaTextInput

class RegisterForm(UserCreationForm):
   captcha=CaptchaField(widget=CaptchaTextInput(attrs={'class': 'form-control'}))

不确定 widget_tweaks 是否与 MultiValueFields 配合得很好,但您也可以在模板中尝试。

{% render_field form.captcha class+="form-control" %}

(注意 += 而不是 =)

或者,再次使用 widget_tweaks

{{ form.captcha|add_class:"form-control" }}