【问题标题】:'tuple' object has no attribute 'fields' using Django Crispy Forms“元组”对象没有使用 Django Crispy Forms 的属性“字段”
【发布时间】:2016-04-29 14:46:29
【问题描述】:

给定一个表单,我想使用 django-crispy-forms 将一些字段包装在 Div 中。

class SignupForm(forms.Form):
  def __init__(self, *args, **kwargs):
    super(SignupForm, self).__init__(*args, **kwargs)
    self.helper = FormHelper()
    self.helper.form_method = 'POST'
    self.helper.form_action = ''
    self.helper.add_input(Submit('submit', 'Create Account', css_class="btn-success"))
    self.helper.layout = Layout(
        Div(
            'username',
            'password',
            css_class="col-md-6"
        ),
        Div(
            'name',
            'age',
            css_class="col-md-4"
        ),
    ),
  username=forms.CharField(max_length=128)
  password=forms.CharField(max_length=128, widget=forms.PasswordInput())
  name=forms.CharField(max_length=128)
  age=forms.IntegerField(required=False)

但是,在视图中,当我使用{% crispy form %} 时,它会抛出错误'tuple' object has no attribute 'fields'

我怀疑我的 Layout 未正确实例化,但无法从此处移动。

【问题讨论】:

    标签: django-crispy-forms


    【解决方案1】:

    自己解决了。对此的解决方案是在 Layout 的末尾有一个额外的逗号。正确的解决方案是:

    self.helper.layout = Layout(
        Div(
            'username',
            'password',
            css_class="col-md-6"
        ),
        Div(
            'name',
            'age',
            css_class="col-md-4"
        ),
    )
    

    去掉最后一行的逗号。

    【讨论】:

    • 谢谢!我自己要花很长时间才能找到这个......
    猜你喜欢
    • 1970-01-01
    • 2016-04-06
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2019-12-31
    • 1970-01-01
    相关资源
    最近更新 更多