更改 Bootstrap Crispy Form 的默认类
我正在使用 Crispy Forms 和 Bootstrap4。目前,所有输入字段均使用 form-control
类呈现。我想将 form-control-lg
添加到输入字段。
我已尝试以下操作,但它不起作用
forms.py
class UserRegisterForm(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2', 'first_name', 'last_name']
help_texts = {
'username': None,
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['password1'].help_text = None
self.fields['password2'].help_text = None
self.fields['password2'].label = "Confirm Password"
self.helper = FormHelper()
self.helper.layout = Layout(
Field(
'username', css_class="form-control form-control-lg my-custom-class"
)
)
Template
<div class="content-section">
<form method="POST" action="">
<h3>Sign Up</h3>
<hr>
{% csrf_token %}
{{ form | crispy }}
</br>
<button type="submit" class="btn btn-primary">Sign Up</button>
</form>
</div>
另外,我可以全局修改所有输入字段的类吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
遵循您已有的模式。我们先从这个方法开始:
1.
在项目 settings.py 中添加此行
因此,从上面你可以用这种方式替换:
来自 Django Crispy Form Doc 的参考和引用
编辑
检查此答案
Following the pattern you already have. Let's start with this method:
1.
Inside project settings.py add this line
So from above you could substitute this way:
References and quotes from Django Crispy Form Doc
Edit
Check this answer