我正在使用django-crispy-forms渲染 Bootstrap3 inline form (代码如下所示),但未显示表单提交时的错误(例如跳过必填字段)。它们采用普通和水平形式布局。
有人可以提出可能的原因吗?
模型.py
class Person(models.Model):
name = models.CharField(max_length=500)
city = models.CharField(max_length=50)
country = models.CharField(max_length=50)
email = models.EmailField(blank=True)
表单.py
class EntryForm(forms.ModelForm):
class Meta:
model = Person
def __init__(self, *args, **kwargs):
super(EntryForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_class = 'form-inline'
self.helper.field_template = 'bootstrap3/layout/inline_field.html'
self.helper.layout.append(ButtonHolder(
Submit('save', 'Save', css_class='btn-primary btn-hg')
)
)
我在模板中使用 {% load Cripy_forms_tags %}
和 {% Crispy form %}
。
最佳答案
原因是您使用的 inline_field.html 模板没有显示错误的代码。
请将普通 bootstrap3/field.html 与内联版本进行比较。您会注意到
{% include 'bootstrap3/layout/help_text_and_errors.html' %}
后者缺少 。将内联更改为如下所示后,您将收到错误消息。
{% load crispy_forms_field %}
{% if field.is_hidden %}
{{ field }}
{% else %}
{% if field|is_checkbox %}
<div id="div_{{ field.auto_id }}" class="checkbox">
<label for="{{ field.id_for_label }}" class="{% if field.field.required %} requiredField{% endif %}">
{% crispy_field field 'class' 'checkbox' %}
{{ field.label|safe }}
{% include 'bootstrap3/layout/help_text_and_errors.html' %}
</label>
</div>
{% else %}
<div id="div_{{ field.auto_id }}" class="form-group">
<label for="{{ field.id_for_label }}" class="sr-only{% if field.field.required %} requiredField{% endif %}">
{{ field.label|safe }}
</label>
{% crispy_field field 'placeholder' field.label %}
{% include 'bootstrap3/layout/help_text_and_errors.html' %}
</div>
{% endif %}
{% endif %}
当然,错误消息非常难看(因为它们来自普通版本),因此您可能必须创建 bootstrap3/layout/help_text_and_errors.html
的内联版本。还可能需要一些 error
css 类 - 请参阅 field.html。
关于django - django-crispy-forms 中的 Bootstrap3 内联表单不显示表单错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26863624/