如何在Django中使用带有脆形的Django欧芹?

时间:2015-06-30 04:51:23

标签: django django-forms django-crispy-forms

我是Django crispy形式的初学者和Django欧芹,我想知道如何添加data-validate =' parsley'在表格标签中。

<form data-parsley-validate>
    {{ form.as_p }}
</form>

我的form.py由以下代码组成。我使用的是Crispy表单。

def __init__(self, *args, **kwargs):
        '''
            Describes how to render the form in the template.
        '''
        super(StudentForm, self).__init__(*args, **kwargs)
        # This is to handle department permissions for department
        self.fields['department'].queryset = Department.objects.all()
        # This is to handle department level permissions for batches
        self.fields['batch'].queryset = Batches.objects.all()
        self.helper = FormHelper(self)
        self.helper.form_id = 'add_student_details'
        self.helper.form_class = 'ajax-student-create-form'
        self.helper.form_method = 'post'
        self.helper.form_action = 'students_add_new_student'
        #self.helper.attrs = {'data-parsley-validate'}
        self.helper.html5_required = True

我需要表格标签应该是这样的,

<form data-validate="parsley" method="post">
</form>

1 个答案:

答案 0 :(得分:1)

引用crispy FormHelper docs

  

<强> ATTRS

     

在1.2.0中添加了一个字典来设置任何类型的表单属性。键中的下划线被翻译成连字符。当您需要设置多个表单属性以保持助手整洁时的推荐方法:

     

y

     

{'id': 'form-id', 'data_id': '/whatever'}

在你的应该是代码:

<form id="form-id" data-id="/whatever" ...>