编程语言
首页 > 编程语言> > python-酥脆的形式和引导程序3没有显示错误消息

python-酥脆的形式和引导程序3没有显示错误消息

作者:互联网

我今天已经花了几个小时尝试和谷歌搜索,但我找不到解决问题的方法:

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

表格

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”,然后调用父表单类的init函数,而没有一些重要的kw args.因此,通常来说:您仅传递了额外的关键字参数,而其他的则被忽略了,例如Form,ModelForm等.

标签:django-crispy-forms,forms,twitter-bootstrap-3,python,django
来源: https://codeday.me/bug/20191119/2040015.html