Bootstrap3 inline forms in django-crispy forms that don't display form errors

I use django-crispy-forms to render Bootstrap3's embedded form (the code shown below), but errors when submitting the form (e.g. missing required fields) are not displayed. They are performed in normal and horizontal forms.

Can anyone suggest possible reasons?

Models.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) 

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') ) ) 

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

+8
django django-crispy-forms
source share
2 answers

The reason is that the inline_field.html template used does not contain code for displaying errors.

Please compare normal bootstrap3 / field.html with the inbuilt version. You will notice that

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

missing in the latter. After you change inline to something like below, you will get error messages.

 {% 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 %} 

Of course, error messages are pretty ugly (since they come from the regular version), so you probably have to create a built-in version of bootstrap3/layout/help_text_and_errors.html . Some error css classes may also be required - see field.html File.

+6
source share

Below is the current configuration for my projects. I think it might work for you too.

 #forms.py from crispy_forms.helper import FormHelper from crispy_forms.layout import Submit, Layout, Div from crispy_forms.bootstrap import FormActions from myapp.models import Person class EntryForm(forms.ModelForm): class Meta: model = Person def __init__(self, *args, **kwargs): super(EntryForm, self).__init__(*args, **kwargs) self.helper = FormHelper() self.helper.form_id = 'id-entryform' self.helper.form_class = 'form-inline' self.helper.form.method = 'post' self.helper.form.action = '' self.helper.layout = Layout( Div('name','email'), Div('country','city'), FormActions(Submit('save', 'Save', css_class='btn-primary btn-hg') ) ) 
+1
source share

All Articles