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

标签 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.'})

最佳答案

弄清楚了。我会把这个留给任何可能感兴趣的人

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

关于Django:ModelForm 自定义错误消息和占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16021334/

相关文章:

Php通过html表单选择行,并写入txt文件

c# - 为什么 C# 中的 Math.Round 舍入和占位符舍入之间存在差异?

python - Django 装置 : Importing Nested Models

python - 如何在 Linux 上使用 venv 设置 Django 环境变量?

python - Django 流式转储数据

javascript - 如何将 React context API 中的变量插入到占位符中?

ckeditor - 类似于 HTML5 的占位符

python - Django 1.11 订单结果问题 - 注释计数返回错误值

javascript - 在 JQuery 中捕获表单输入

php - 登录表单中的未定义索引错误