Django:ModelForm自定义错误消息和占位符

时间:2013-04-15 17:35:53

标签: django forms placeholder django-forms

我遇到的问题是只能有自定义错误消息或占位符文本。一旦将自定义错误消息添加到ModelForm中,占位符文本就不再显示 - 顺序不会影响结果。在此先感谢您的任何帮助和建议!

class LetterForm(ModelForm):
    name = forms.CharField()
    class Meta:
       model = Letter
       exclude = ('date_submitted', 'read', 'approved', 'post', 'date_post', 'url', 'tags',)
       widgets = {
        'youtube': forms.TextInput(attrs={'placeholder': 'youtube'}),
        'name': forms.TextInput(attrs={'placeholder': 'name'}),
        'location': forms.TextInput(attrs={'placeholder': 'location'}),
        'email': forms.TextInput(attrs={'placeholder': 'email'}),
    }
    name = forms.CharField(error_messages={'required': 'Don\'t want to share your real name?'
                                                       ' Just enter Anonymous.'})

1 个答案:

答案 0 :(得分:3)

想出来。我会把这个留给那些可能感兴趣的人

name = forms.CharField(error_messages={'required': 'message'},
                       widget=forms.TextInput(attrs={'placeholder': 'Name'}))