Bootstrap3内联表单中没有显示表单错误的django-crispy表单 [英] Bootstrap3 inline forms in django-crispy-forms not showing form errors

查看:111
本文介绍了Bootstrap3内联表单中没有显示表单错误的django-crispy表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 django-crispy-forms 来呈现Bootstrap3内联表单(代码如下),但表单提交后的错误(如跳过必填字段)是没有显示他们以正常和横向的形式布局。

I am using django-crispy-forms to render a Bootstrap3 inline form (code shown below), but the errors upon form submission (like skipping required fields) are not being shown. They do in normal and horizontal form layouts.

有人可以提出可能的原因吗?

Could someone please suggest the possible reason(s)?

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)



Forms.py



Forms.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 crispy_forms_tags%} {%crispy form%} 在我的模板中。

I am using {% load crispy_forms_tags %} and {% crispy form %} in my template.

推荐答案

原因是您使用的inline_field.html模板没有显示错误的代码。

The reason is that the inline_field.html template that you use, doesn't have a code to display errors.

请比较正常的bootstrap3 / field .html到内联版本。您会注意到

Please compare normal bootstrap3/field.html to the inline version. You will notice that the

{% include 'bootstrap3/layout/help_text_and_errors.html' %}

。将内联更改为下面的内容后,您将收到错误消息。

is missing in the latter. After you change the inline to something like below you'll have error messages back.

{% 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 。也可能需要一些错误 css类 - 请参阅field.html。

Of course error messages are quite ugly (as they come from normal version) so you probably have to create inline version of the bootstrap3/layout/help_text_and_errors.html. Also some error css classes might be needed - see the field.html.

这篇关于Bootstrap3内联表单中没有显示表单错误的django-crispy表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆