没有显示脆皮表格和 bootstrap 3 的错误消息
No error message shown with crispy forms and bootstrap 3
我今天花了几个小时尝试和谷歌搜索,我就是找不到解决我问题的方法:
我在 1.4.0 版和 Bootstrap3 中使用 crispy forms。我有一个如下所示的 CreateView,它在 crispy 表单的帮助下显示了一个表单。 Bootstrap3 的源代码似乎也可以加载。 'name' 字段是必需的。
无论我在这三个字段中输入什么(或者如果我将它们完全留空),每次我点击 "Save" 按钮时都会重新加载表单。没有错误消息出现(例如,对于必填的名称字段)。
这似乎与酥脆的形式有关。因为如果我不使用脆皮表格,我会在名称字段上方收到 "this field is required" 消息。
我只是不明白:我在这里错过了什么?
我遇到了 this post,但这并不完全适合我的情况,因为我没有使用 self.helper.field_template 变量。
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 %}
在 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__'
您只传递了一个 kw 参数 - "form_action",并且在没有一些重要的 kw 参数的情况下调用父表单 class 的 init 函数。所以一般来说:你只传递了额外的关键字参数,而你忘记了其他的 - 来自 Form,ModelForm 等......
我今天花了几个小时尝试和谷歌搜索,我就是找不到解决我问题的方法:
我在 1.4.0 版和 Bootstrap3 中使用 crispy forms。我有一个如下所示的 CreateView,它在 crispy 表单的帮助下显示了一个表单。 Bootstrap3 的源代码似乎也可以加载。 'name' 字段是必需的。
无论我在这三个字段中输入什么(或者如果我将它们完全留空),每次我点击 "Save" 按钮时都会重新加载表单。没有错误消息出现(例如,对于必填的名称字段)。 这似乎与酥脆的形式有关。因为如果我不使用脆皮表格,我会在名称字段上方收到 "this field is required" 消息。
我只是不明白:我在这里错过了什么? 我遇到了 this post,但这并不完全适合我的情况,因为我没有使用 self.helper.field_template 变量。
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 %}
在 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__'
您只传递了一个 kw 参数 - "form_action",并且在没有一些重要的 kw 参数的情况下调用父表单 class 的 init 函数。所以一般来说:你只传递了额外的关键字参数,而你忘记了其他的 - 来自 Form,ModelForm 等......