没有显示带有脆弱表单和引导程序3

时间:2015-07-31 16:48:42

标签: python django forms twitter-bootstrap-3 django-crispy-forms

我今天花了好几个小时尝试和Google搜索,我找不到解决问题的方法:

我在版本1.4.0和Bootstrap3中使用了脆弱的表单。我有一个CreateView,如下所示,它显示了一个表单,有助于形式。 Bootstrap3的来源似乎也加载了。名字的字段是必需的。

无论我在三个字段中输入什么(或者如果我将它们完全留空),每次点击“保存”按钮时都会重新加载表单。不会显示任何错误消息(例如,对于所需的名称字段)。 这似乎与酥脆的形式有关。因为如果我将脆弱的表格留下来,我会在名称字段上方显示“此字段是必填的”消息。

我只是不明白:我在这里错过了什么? 我遇到了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 %}

1 个答案:

答案 0 :(得分:3)

在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”,并调用父表单类的init函数,而没有一些重要的kw args。所以一般情况下:你只传递额外的关键字参数,而你忘记了其他的 - 来自Form,ModelForm等......