如何调整Django表单中help_texts的大小?

问题描述 投票:0回答:1

我正在尝试调整“用户名”的 help_texts 字段的大小。我不明白应该在哪里应用样式。

class SignUpForm(UserCreationForm):
    
    password1 = forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control', 'placeholder':'Password'}), label='')
    password2 = forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control', 'placeholder':'Confirm Password'}), label='')
    
    class Meta:
        
        model = User
        fields = ['username', 'email', 'password1', 'password2', 'phone']
        
        widgets = {
            'username': forms.TextInput(attrs={'class':'form-control', 'placeholder':'Username'}),
            'email': forms.EmailInput(attrs={'class':'form-control', 'placeholder':'Email Address'}),
            'phone': forms.TextInput(attrs={'class':'form-control', 'placeholder':'Phone Number'}),
        }
        
        labels = {
            'username': '',
            'email': '',
            'phone': ''
        }
        
        help_texts = {
            "username": "Must contain only letters, numbers, hyphens and underscores.",
        }
django django-forms text-styling
1个回答
0
投票

您可以尝试覆盖您的

help_text

class SignUpForm(UserCreationForm):
    ...
    ...
    
    def __init__(self, *args, **kwargs):
            super(SignUpForm, self).__init__(*args, **kwargs)
            self.fields['username'].help_text = (
                '<span style="color: red; font-size: 12px;">'
                f'{self.fields["username"].help_text}</span>'
            )
© www.soinside.com 2019 - 2024. All rights reserved.