更改字段名称django在显示窗体错误消息时使用 [英] changing the field name django uses when display form error messages
问题描述
{%if form.errors %}
{form.errors%}
{%endif%}
现在,假设我的表单中有一个用户名字段,这是必需的,我故意不填写。它会说
用户名
- 此字段为必需
如何更改在显示错误信息的字段时显示的username一词?我猜这是从我创建的模型中取名,因为在模型中(我曾经写过一个表单)我写了
username = models.CharField(max_length = 10)
first_name = models.CharField(max_length = 20)
如您所见,如果first_name字段有错误,则django将显示
first_name
- *错误消息*
如何使其显示名字的first_name?
如果你想把一些自定义消息给你的错误,你可能想修改干净的方法您的表单。
例如:
class Form(forms。 ModelForm):
class Meta:
model = User #Assuming User是您的模型的名称
fields = ['username','first_name']
def clean_first_name(self):
if condition: #一个必须引发错误的条件
raise forms.ValidationError(u'你的自定义验证错误在这里。)
return self.cleaned_data ['first_name']
但是,如果您只想更改出现在默认错误消息中的名称,您可以在字段中放置一个详细的名称,如: / p>
username = models.CharField(max_length = 10,verbose_name =Username)
first_name = models.CharField(max_length = 20,verbose_name =名字)
或隐式:
username = models.CharField(Username,max_length = 10)
first_name = models.CharField(First Name,max_length = 20)
要查看有关django表单和字段验证的更多信息: https://docs.djangoproject.com/en/1.5/ref/forms/validation/#形状和音响密码验证
关于覆盖默认表单错误消息:
Django覆盖默认表单错误消息
和
http://davedash.com / 2008/11/28 / custom-error-messages-for-django-forms /
Okay so I have a form and then in the template, I have this code:
{% if form.errors %}
{ form.errors %}
{% endif %}
Now, suppose I have a username field in my form which is required and I purposely do not fill it out. It would say
username
-This field is required
How do I change the word "username" which it displays when telling what field the error message is for? I'm guessing it is taking the name from the Model I created because in the model (which I then made a form of) I had written
username = models.CharField(max_length=10)
first_name = models.CharField(max_length=20)
As you can see, if there is an error with the first_name field, it django would display
first_name
- *error message*
How would I make it display "First Name" instead of "first_name"?
If you want to put some custom messages to your erros you may want to modify the clean method of your formfields.
For example:
class Form(forms.ModelForm):
class Meta:
model = User #Assuming User is the name of your model
fields = ['username','first_name']
def clean_first_name(self):
if condition: # A condition that must raise an error
raise forms.ValidationError(u'Your Custom Validation Error goes here.')
return self.cleaned_data['first_name']
But if you just want to change the name that appears in a default error message you can put a verbose name in your fields, just like:
username = models.CharField(max_length=10, verbose_name="Username")
first_name = models.CharField(max_length=20, verbose_name="First Name")
or implicit:
username = models.CharField("Username", max_length=10)
first_name = models.CharField("First Name",max_length=20)
To see more about django form and field validation: https://docs.djangoproject.com/en/1.5/ref/forms/validation/#form-and-field-validation
About Override default Form error messages: Django override default form error messages
and
http://davedash.com/2008/11/28/custom-error-messages-for-django-forms/
这篇关于更改字段名称django在显示窗体错误消息时使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!