简体   繁体   English

在 Django 视图中添加的字段未使用 django-crispy-forms 显示

[英]fields added in Django View not displayed with django-crispy-forms

I have this view (modified to make it simple to understand):我有这个观点(修改以使其易于理解):

class MyCreateViewView(CreateView):

    model = SomeModel

    def get_form(self, form_class=None):
        form = super().get_form(form_class)
        form.helper = FormHelper(form)

        form.fields['additional_field'] = forms.BooleanField(label="Something", required=False)

        form.helper.add_input(Submit('submit', "Submit"))

        return form

Before adding the FormHelper , the field additional_field appeared on the template.在添加FormHelper之前,字段additional_field出现在模板上。 Now it doesn't.现在没有了。 The form in my template is basically one line: {% crispy form %} .我的模板中的表单基本上是一行: {% crispy form %}

Thedjango-crispy-forms docs don't show this approach because they are focused on using forms. django-crispy-forms 文档没有展示这种方法,因为它们专注于使用 forms。 I'm using the CreateView from Django that builds the form for me, so I don't need a form class.我正在使用 Django 的 CreateView 为我构建表单,因此我不需要 class 表单。

So far, I noticed that doing form.helper = FormHelper(form) after programmatically adding the new field solves the problem.到目前为止,我注意到在以编程方式添加新字段之后执行form.helper = FormHelper(form)可以解决问题。 But this is not a solution because the view I presented here is a simplified version, and I actually have two views that are doing something similar.但这不是一个解决方案,因为我在这里展示的视图是一个简化版本,实际上我有两个视图在做类似的事情。 So I'm inheriting this class and adding extra fields in the views themselves (meaning FormHelper is already there).所以我继承了这个 class 并在视图本身中添加了额外的字段(意味着 FormHelper 已经存在)。

Solution: form.helper = FormHelper() (notice I'm passing the form as an argument)解决方案: form.helper = FormHelper() (注意我将表单作为参数传递)

Using an IDE led me to this error because the class is FormHelper(form=None) so having a form argument that I have, made it easy to fell into this trap.使用 IDE 导致我出现此错误,因为 class 是FormHelper(form=None)所以我有一个form参数,很容易陷入这个陷阱。 In my opinion, this is not well explained, and you have to dig into the code to understand that passing the form actually builds a Layout (maybe form_for_layout would be a better naming or simply adding form to the docstring - I created an issue with this suggestion, and will update this answer if it is implemented):在我看来,这并没有得到很好的解释,您必须深入研究代码才能了解传递表单实际上构建了一个布局(也许form_for_layout会是一个更好的命名或只是将form添加到文档字符串 - 我用这个创建了一个问题建议,如果实施将更新此答案):

if form is not None:
    self.form = form
    self.layout = self.build_default_layout(form)

For someone new to django-crispy-forms (my case), this complicates things because it is actually defining the way the form fields are rendered.对于刚接触 django-crispy-forms 的人(我的例子),这会使事情变得复杂,因为它实际上定义了表单字段的呈现方式。 This is why adding the FormHelper after adding the field showed no problem (it added the field to the layout,).这就是为什么在添加字段后添加 FormHelper 没有问题(它将字段添加到布局中)。 but doing it the other way around didn't work.但反过来做是行不通的。

Understanding the problem was in layout led me to other solutions.了解问题出在布局中,使我找到了其他解决方案。 If you need layout, form.helper.layout = form.helper.build_default_layout(form) (or del form.helper.layout ) after adding the field also works, but you will lose any changes you made to the Layout.如果您需要布局,添加字段后form.helper.layout = form.helper.build_default_layout(form) (或del form.helper.layout )也可以,但您将丢失对布局所做的任何更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM