Django Crispy Forms CustomLayout with Fieldset
所以我想构建一个自定义布局,它扩展了我拥有的表单的Django Crispy Forms CustomLayout with Fieldset,django,django-crispy-forms,Django,Django Crispy Forms,所以我想构建一个自定义布局,它扩展了我拥有的表单的LayoutObject class NewBookForm(LayoutObject): template = 'library/layouts/new_book_layout.html' def __init__(self, fields, template=None, *args, **kwargs): self.fields = fields # Overrides class var
LayoutObject
class NewBookForm(LayoutObject):
template = 'library/layouts/new_book_layout.html'
def __init__(self, fields, template=None, *args, **kwargs):
self.fields = fields
# Overrides class variable with an instance level variable
if template:
self.template = template
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK, **kwargs):
fields = self.get_rendered_fields(form, form_style, context, template_pack, **kwargs)
template = self.get_template_name(template_pack)
return render_to_string(template, {'fields': fields})
我称之为使用
self.helper.layout = Layout(
NewBookForm(Fieldset('book_id', 'name', 'author'))
)
现在Django说“模板不存在。”
然而,这并没有让我得到我想要的结果
更新1:
现在library/layouts/new_book_layout.html有如下内容
<div class="w-40 mr-2">
{{name|as_crispy_field}}
{{name.errors.as_ul }}
{{author|as_crispy_field}}
{{author.errors.as_ul }}
</div>
并强调:
{{name|as_crispy_field}}
这是因为一旦调用
get_rendered_fields
它将返回一个字符串对象,而不是crispy对象,因此不使用|as_crispy_field
过滤器,您应该使用|safe
过滤器,因为您的上下文包含的是一个HTML字符串。您是否可以包含stacktrace。对问题进行编辑以添加您的问题
{{name|as_crispy_field}}