在Django Crispy Form中如何使用`layout.append(HTML(“<p>无论</p>”))

时间:2013-06-04 14:26:35

标签: django-crispy-forms

我在阅读文档时阅读了关于crispy表单的教程。我在https://django-crispy-forms.readthedocs.org/en/d-0/dynamic_layouts.html

中的操作布局中发现了一些非常有用的内容
layout.append(HTML("<p>whatever</p>"))
layout.insert(1, HTML("<p>whatever</p>"))

所以我试过了:

class PostJobForm(forms.ModelForm):    
    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_tag = False

        self.helper.layout.insert(1,  HTML("<p>Drag and drop photos and video here</p><p class='big'>+</p><p>Or click to add using the file browser</p>"),)

        super(PostJobForm, self).__init__(*args, **kwargs)
    class Meta:
        model=Job
        exclude=['employer','hitcount','location']

我收到了错误

'NoneType' object has no attribute 'insert'

这个布局对象在哪里?它没有在教程中提到。

我如何使用layout.insert(1, HTML("<p>whatever</p>"))

1 个答案:

答案 0 :(得分:1)

  

super(PostJobForm, self).__init__(*args, **kwargs)必须放在init对象下面。并为函数FormHelper()添加self。

class PostJobForm(forms.ModelForm):    
    def __init__(self, *args, **kwargs):
        super(PostJobForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.form_tag = False