django脆皮表单覆盖布局对象模板被忽略
我应用了此
因此我有
I applied this thus I have
self.helper.layout = Layout(
Field(
'title', template="mytemplate.html"
) ,
模板未呈现
helper.field_template在以下代码中为None :
(C:\myapp\lib\ crispy_forms\templatetags\crispy_forms_filters.py):
(C:\myapp\lib\crispy_forms\templatetags\crispy_forms_filters.py):
@register.filter(name='as_crispy_field')
def as_crispy_field(field, template_pack=TEMPLATE_PACK, label_class="", field_class=""):
"""
Renders a form field like a django-crispy-forms field::
{% load crispy_forms_tags %}
{{ form.field|as_crispy_field }}
or::
{{ form.field|as_crispy_field:"bootstrap" }}
"""
if not isinstance(field, forms.BoundField) and settings.DEBUG:
raise CrispyError('|as_crispy_field got passed an invalid or inexistent field')
attributes = {
'field': field,
'form_show_errors': True,
'form_show_labels': True,
'label_class': label_class,
'field_class': field_class,
}
helper = getattr(field.form, 'helper', None)
template_path = None
if helper is not None:
attributes.update(helper.get_attributes(template_pack))
template_path = helper.field_template
if not template_path:
template_path = '%s/field.html' % template_pack
template = get_template(template_path)
c = Context(attributes).flatten()
return template.render(c)
如果我在调试时将helper.field_name修改为mytemplate.html,我会看到它成功呈现。
if I modify helper.field_name on debug to mytemplate.html, I see it is rendered with success.
问题是什么是我的模板被忽略的原因?
Question is what could be reason that my template is ignored?
重要说明,我的表单扩展了:
class RoomForm(ModelForm)
其中 ModelForm
为此处
在我的表单中,我用以下方式呈现:
In my form I render with:
{{ form.title | as_crispy_field }}
我的观点的相关部分是:
and relevant part of my view is:
form = RoomForm(None, prefix="submit-room" )
return render(request, 'edit_room.html', { 'form': form })
最终将mytemplate.html复制为无处不在: C:\myapp\lib\crispy_forms\模板bootstrap4
和 C:\myapp\templates\mytemplate.html
finally mytemplate.html is, copied "everywhere":
C:\myapp\lib\crispy_forms\templates\bootstrap4
and C:\myapp\templates\mytemplate.html
{% load custom_tags %}
<div>tutu</div>
<div>{{field}}</div>
我不想将问题目的改变很多到目前为止我可以用输入来回答它:
I don't want to change question purpose to much that I can answer it with the inputs so far I got:
self.helper.layout = Layout(
Field(
'title', template="mytemplate.html"
)
成功设置单个字段模板,但 as_crispy_field
不在乎,但它采用表单模板值。
Successfully sets individual fields template but as_crispy_field
does not care it but it takes form template value instead.
如果我渲染
{% crispy form %}