Django默认情况下为每个字段添加属性

时间:2014-07-09 20:50:24

标签: python django

我需要指出正确的方向。我的脆弱形式'渲染我用于AngularJS的表单。我希望默认情况下将属性ng-model="NAME"添加到所有表单字段。

我想这可以使用添加到我的表单中的mixin来完成,即AngularJSFormMixin

 class ProfileAuthenticationForm(AngularJSFormMixin, AuthenticationForm):


    def __init__(self, *args, **kwargs):
        super(ProfileAuthenticationForm, self).__init__(*args, **kwargs)

        self.helper = FormHelper()
        self.helper.form_method = 'post'
        # turn off HTML5 validation
        self.helper.attrs = {'novalidate': ''}
        self.helper.form_show_labels = False

        self.helper.layout = Layout(
            Field('username',  placeholder="E-mail", autocomplete='off'),
            Field('password', placeholder="Password", autocomplete='off'),
            HTML('<input type="submit" name="Login" ng-click="submit()" css_class="btn btn-primary btn-block" />'),

        )

但是我不确定在这里使用AngularJSFormMixin做什么。有没有办法在默认情况下自动将ng-model="NAME"添加到每个字段?

1 个答案:

答案 0 :(得分:1)

我之前从未使用过脆形式,但这是我在模型表单__init__上所做的,以便为其添加一个ng-model属性。我的错误元素&#39; directive用于向用户显示表单错误:

  def __init__(self, *args, **kwargs):                                      
      super(MyForm, self).__init__(*args, **kwargs)                     

      for field in self:                                                    

          field.field.widget.attrs.update({'ng-focus': ''})                 

          field.field.widget.attrs.update({                             
              'ng-model': 'formName.{0}'.format(field.name),            
              'get-error-elements': '',                                 
          })