No error messages with crispy forms and bootstrap 3

I spent hours today testing and searching the web, I just can't seem to find a solution for my problem:

I am using crunchy shapes in 1.4.0 and Bootstrap3. I have a CreateView as shown below that displays a form using crispy shapes. The sources for Bootstrap3 seem to be loading too. You must enter a name in the field.

Regardless of whether I enter three fields (or if I left them completely blank), the form reloads every time I click the Save button. No error message appears (for example, for a required name field). It seems to have something to do with crispy forms. Because if I leave crunchy forms I get a "this field is required" message above the name field.

I just don't understand: what am I missing here? I came across this post but it doesn't fit my case as I am not using the self.helper.field_template variable.

models.py

class SomeItem(models.Model):
    name = models.CharField(_('Some item name'), max_length=30)
    longitude = models.DecimalField(_('Longitude'), max_digits=9, decimal_places=7, blank=True, null=True,
                                help_text=_('Longitude values range from -90 to 90'))
    latitude = models.DecimalField(_('latitude'), max_digits=9, decimal_places=7, blank=True, null=True,
                                help_text=_('Latitude values range from -180 to 180'))

      

forms.py

class CrispyForm(ModelForm):
'''
This form serves as a generic form for adding and editing items.
'''
def __init__(self, *args, **kwargs):
    form_action = kwargs.pop('form_action', None)
    super(CrispyForm, self).__init__(*args, **kwargs)

    self.helper = FormHelper(self)

    # Form attributes
    self.helper.form_method = 'post'
    self.helper.form_action = reverse(form_action)
    self.helper.form_class = 'form-horizontal'
    self.helper.label_class = 'col-lg-2'
    self.helper.field_class = 'col-lg-10'

    # Save button, having an offset to align with field_class
    save_text = _('Save')
    self.helper.layout.append(Submit('save_form', save_text, css_class="btn btn-primary col-sm-offset-2"))


class SomeItemAddForm(CrispyForm):
    def __init__(self, *args, **kwargs):
        super(SomeItemAddForm, self).__init__(*args, form_action='add-someitem')

    class Meta:
        model = SomeItem
        fields = '__all__'

      

views.py

class SomeItemAddView(CreateView):
    template_name = 'add_someitem.html'
    form_class = SomeItemAddForm
    model = SomeItem
    success_url = reverse_lazy('someitmes')

class ListSomeItemsView(ListView):
    model = SomeItem
    template_name = 'list_someitems.html'

      

urls.py

urlpatterns = [
    url(r'^someitems/add$', SomeItemAddView.as_view(), name='add-someitem'),
    url(r'^someitems$', ListSomeItemsView.as_view(), name='someitems'),
]

      

add_someitem.html

{% extends "base.html" %}
{% load i18n %}
{% load crispy_forms_tags %}

{% block content %}
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    {% crispy form %}
                </div>
            </div>
        </div>
    </div>
{% endblock content %}

      

+3


source to share


1 answer


Change this in form.py.

class SomeItemAddForm(CrispyForm):
    def __init__(self, *args, **kwargs):
        super(SomeItemAddForm, self).__init__(*args, form_action='add-someitem', **kwargs)

    class Meta:
        model = SomeItem
        fields = '__all__'

      



You pass only one kw argument, "form_action", and call the init function of the parent class of the form without any important kw arguments. So, in general: you only pass the optional keyword argument and you forget the others - from the form, ModelForm, etc.

+3


source







All Articles