问题
I applied this thus I have
self.helper.layout = Layout(
Field(
'title', template="mytemplate.html"
) ,
As result my template is not rendered helper.field_template is None in below code:
(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)
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?
Important note, my form extends:
class RoomForm(ModelForm)
where ModelForm
is as here
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 })
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>
回答1:
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"
)
Successfully sets individual fields template but as_crispy_field
does not care it but it takes form template value instead.
If I render as form with, it works perfectly.
{% crispy form %}
来源:https://stackoverflow.com/questions/53412634/django-crispy-forms-overriding-layout-objects-templates-ignored