【发布时间】:2021-09-12 19:37:07
【问题描述】:
我正在尝试覆盖默认错误消息,这样我就可以自己为错误定义一些句子,但我无法覆盖,这真的浪费了我的时间。有人知道我错过了什么吗?
from django import forms
from django.forms import ModelForm
from django.utils.translation import ugettext_lazy
from .models import *
# Create your models here.
class ProfileForm(ModelForm):
error_messages = {
'required': ugettext_lazy("This field is mandatory."),
'invalid': 'Enter a valid number',
'caps': 'This field if case sensitive'
}
esewa_id = forms.CharField(error_messages=error_messages,
label='Esewa_Id',
help_text='This information will be used only for payment purpose.',
widget=forms.NumberInput(attrs={'class':'NumberField', 'type':'number', 'placeholder':'Enter your Esewa_Id'}),
)
class Meta:
model=Customer
fields = ['first_name','second_name','email','image','gender','esewa_id']
在 models.py 中,我定义了一个继承自 User 模型的 Customer 模型,并且还使用验证器进行图像验证,并且确实提到了 help_text。
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from django.core.validators import FileExtensionValidator,MinValueValidator,MaxValueValidator
# Create your models here.
class Customer(models.Model):
Gender_Choices=[
('Male','Male'),
('Female','Female'),
]
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
first_name = models.SlugField(max_length=10, null=True)
second_name=models.CharField(max_length=10,null=True)
email = models.EmailField(max_length=140, null=True,blank=True)
image = models.ImageField(upload_to="thumbnails/profile/%y"
,help_text="This will be uploaded as your profile picture. Only .png and .jpg are accepted"
,validators=[FileExtensionValidator(['png','jpg'])]
,blank=True,null=True)
gender=models.CharField(choices=Gender_Choices,null=True,max_length=50)
esewa_id=models.PositiveIntegerField(null=True
,help_text='This information will be used only for payment purpose.'
,validators=[MinValueValidator(9400000000),MaxValueValidator(9999999999)]
)
auth_token = models.CharField(max_length=100,null=True)
is_verified = models.BooleanField(default=False)
def __str__(self):
return str(self.user)
【问题讨论】:
标签: python django visual-studio-code