Reputation: 167
I have spent hours today trying out and googling, I just can't find the solution for my problem:
I use crispy forms in version 1.4.0 and Bootstrap3. I have a CreateView as shown below which displays a form with the help of crispy forms. The sources for Bootstrap3 seem to load as well. The 'name' field is required.
No matter what I enter in the three fields (or if I leave them completely blank), the form is reloaded every time I hit the "Save" button. No error message appears (e.g. for the required name field). It seems to have to do with the crispy forms. Because if I leave crispy forms out, I get the "this field is required" message above the name field.
I just don't get it: what am I missing here? I came across this post, but this does not fit exactly to my case as I don't use 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 %}
Upvotes: 3
Views: 1574
Reputation: 852
Change this in the forms.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 form class without some important kw args. So in general: You pass only the extra keyword argument, and you are forgot the others - from Form, ModelForm, etc ...
Upvotes: 3