Django 1.9 无法将表单字段留空问题
Django 1.9 Can't leave formfield blank issue
我修改了表单以显示模型中字段的新标签。现在我不能让用户将表单字段留空。我尝试添加 blank=True 和 null=True,但没有成功。如何允许字段为空?
我的模特:
class UserProfile(models.Model):
# Page 1
user = models.OneToOneField(User)
first_name = models.CharField(max_length=100, blank=True)
last_name = models.CharField(max_length=100, blank=True)
email = models.EmailField(max_length=100, blank=True, unique=True)
# Page 2 Address
line1 = models.CharField(max_length=255, blank=True, null=True)
line2 = models.CharField(max_length=255, blank=True, null=True)
line3 = models.CharField(max_length=255, blank=True, null=True)
city = models.CharField(max_length=255, blank=True, null=True)
state = models.CharField(max_length=2, blank=True, null=True)
zip_code = models.CharField(max_length=15, blank=True, null=True)
def __str__(self):
return self.user.username
forms.py:
class UserProfileForm3(forms.ModelForm):
line1 = forms.CharField(label="Address")
line2 = forms.CharField(label="")
line3 = forms.CharField(label="")
city = forms.CharField(label="City / Town")
state = forms.CharField(label="State / Province / Region")
zip_code = forms.IntegerField(label="Zip / Postal Code")
def __init__(self, *args, **kwargs):
super(UserProfileForm3, self).__init__(*args, **kwargs)
self.fields['line1'].widget.attrs = {
'class': 'form-control',
'placeholder': 'Address Line 1'
}
self.fields['line2'].widget.attrs = {
'class': 'form-control',
'placeholder': 'Address Line 2'
}
self.fields['line3'].widget.attrs= {
'class': 'form-control',
'placeholder': 'Address Line 3'
}
self.fields['city'].widget.attrs = {
'class': 'form-control',
'placeholder': 'City'
}
self.fields['state'].widget.attrs = {
'id': 'state_id',
'class': 'form-control',
'placeholder': 'State'
}
self.fields['zip_code'].widget.attrs = {
'id': 'zip_code_id',
'class': 'form-control',
'placeholder': 'Zip'
}
self.fields['country'].widget.attrs = {
'class': 'form-control',
'placeholder': 'US',
'value': 'US'
}
class Meta:
model = UserProfile
fields = ('line1', 'line2', 'line3', 'city', 'state', 'zip_code','country')
您覆盖了字段,因此它们不会保留任何属性;您需要明确设置它们。
line1 = forms.CharField(label="Address", required=False)
另请注意,您的模型字段定义不应该 null=True
用于 CharFields,这是不好的做法。
尝试在 forms.py 中添加:
self.fields['line1'].required = false
您必须为每个字段执行此操作。
我修改了表单以显示模型中字段的新标签。现在我不能让用户将表单字段留空。我尝试添加 blank=True 和 null=True,但没有成功。如何允许字段为空?
我的模特:
class UserProfile(models.Model):
# Page 1
user = models.OneToOneField(User)
first_name = models.CharField(max_length=100, blank=True)
last_name = models.CharField(max_length=100, blank=True)
email = models.EmailField(max_length=100, blank=True, unique=True)
# Page 2 Address
line1 = models.CharField(max_length=255, blank=True, null=True)
line2 = models.CharField(max_length=255, blank=True, null=True)
line3 = models.CharField(max_length=255, blank=True, null=True)
city = models.CharField(max_length=255, blank=True, null=True)
state = models.CharField(max_length=2, blank=True, null=True)
zip_code = models.CharField(max_length=15, blank=True, null=True)
def __str__(self):
return self.user.username
forms.py:
class UserProfileForm3(forms.ModelForm):
line1 = forms.CharField(label="Address")
line2 = forms.CharField(label="")
line3 = forms.CharField(label="")
city = forms.CharField(label="City / Town")
state = forms.CharField(label="State / Province / Region")
zip_code = forms.IntegerField(label="Zip / Postal Code")
def __init__(self, *args, **kwargs):
super(UserProfileForm3, self).__init__(*args, **kwargs)
self.fields['line1'].widget.attrs = {
'class': 'form-control',
'placeholder': 'Address Line 1'
}
self.fields['line2'].widget.attrs = {
'class': 'form-control',
'placeholder': 'Address Line 2'
}
self.fields['line3'].widget.attrs= {
'class': 'form-control',
'placeholder': 'Address Line 3'
}
self.fields['city'].widget.attrs = {
'class': 'form-control',
'placeholder': 'City'
}
self.fields['state'].widget.attrs = {
'id': 'state_id',
'class': 'form-control',
'placeholder': 'State'
}
self.fields['zip_code'].widget.attrs = {
'id': 'zip_code_id',
'class': 'form-control',
'placeholder': 'Zip'
}
self.fields['country'].widget.attrs = {
'class': 'form-control',
'placeholder': 'US',
'value': 'US'
}
class Meta:
model = UserProfile
fields = ('line1', 'line2', 'line3', 'city', 'state', 'zip_code','country')
您覆盖了字段,因此它们不会保留任何属性;您需要明确设置它们。
line1 = forms.CharField(label="Address", required=False)
另请注意,您的模型字段定义不应该 null=True
用于 CharFields,这是不好的做法。
尝试在 forms.py 中添加:
self.fields['line1'].required = false
您必须为每个字段执行此操作。