【问题标题】:Django 1.9 Can't leave formfield blank issueDjango 1.9 无法将表单域留空问题
【发布时间】:2017-02-01 10:39:40
【问题描述】:

我修改了我的表单,为我的模型中的字段显示一个新标签。现在我不能让用户将表单字段留空。我试图添加空白=真和空=真,但没有运气。如何允许字段为空白?

我的模特:

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')

【问题讨论】:

    标签: python django django-models django-forms django-1.9


    【解决方案1】:

    您覆盖了这些字段,因此它们不会保留任何属性;您需要明确设置它们。

    line1 = forms.CharField(label="Address", required=False)
    

    另请注意,您的模型字段定义不应该有 null=True 用于 CharFields,这是不好的做法。

    【讨论】:

    • 感谢 Daniel,感谢您分享 null=True 是不好的做法。我将检查编写 CharFields 的正确做法并纠正我的模型。我没有意识到这不是要做的事!老实说,我对 Django 很陌生,即使阅读文档也会让我感到困惑。
    • 参见the docs:“避免在基于字符串的字段(如 CharField 和 TextField)上使用 null,因为空字符串值将始终存储为空字符串,而不是 NULL。如果基于字符串的字段具有null=True,这意味着它有两个可能的“无数据”值:NULL,和空字符串。在大多数情况下,有两个可能的“无数据”值是多余的; Django 约定是使用空字符串,而不是 NULL。”
    【解决方案2】:

    尝试在forms.py中添加这个:

    self.fields['line1'].required = false
    

    您必须为每个字段执行此操作。

    【讨论】:

    • 你和丹尼尔都工作了。但是,有了你的,我还可以编写一个 clean_data 函数来编写我自己的验证错误。另一种方法,不会让我这么简单。
    猜你喜欢
    • 2017-02-02
    • 2016-07-29
    • 2015-07-19
    • 1970-01-01
    • 2011-09-08
    • 2016-04-26
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    相关资源
    最近更新 更多