Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python crispy forms和bootstrap 3没有显示错误消息_Python_Django_Forms_Twitter Bootstrap 3_Django Crispy Forms - Fatal编程技术网

Python crispy forms和bootstrap 3没有显示错误消息

Python crispy forms和bootstrap 3没有显示错误消息,python,django,forms,twitter-bootstrap-3,django-crispy-forms,Python,Django,Forms,Twitter Bootstrap 3,Django Crispy Forms,我今天花了几个小时尝试和谷歌搜索,我就是找不到解决问题的方法: 我在版本1.4.0和Bootstrap3中使用了crispy表单。我有一个如下所示的CreateView,它在crispy表单的帮助下显示一个表单。Bootstrap3的源代码似乎也在加载。“名称”字段是必需的 无论我在这三个字段中输入什么(或者如果我将它们完全留空),每次我点击“保存”按钮时都会重新加载表单。没有出现错误消息(例如,对于所需的名称字段)。 这似乎与酥脆的形状有关。因为如果我不使用crispy表单,我会在name字段

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

我在版本1.4.0和Bootstrap3中使用了crispy表单。我有一个如下所示的CreateView,它在crispy表单的帮助下显示一个表单。Bootstrap3的源代码似乎也在加载。“名称”字段是必需的

无论我在这三个字段中输入什么(或者如果我将它们完全留空),每次我点击“保存”按钮时都会重新加载表单。没有出现错误消息(例如,对于所需的名称字段)。 这似乎与酥脆的形状有关。因为如果我不使用crispy表单,我会在name字段上方得到“this field is required”消息

我只是不明白:我在这里错过了什么? 我遇到过,但这并不完全适合我的情况,因为我没有使用self.helper.field_模板变量

型号.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__'
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'
urlpatterns = [
    url(r'^someitems/add$', SomeItemAddView.as_view(), name='add-someitem'),
    url(r'^someitems$', ListSomeItemsView.as_view(), name='someitems'),
]
forms.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__'
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'
urlpatterns = [
    url(r'^someitems/add$', SomeItemAddView.as_view(), name='add-someitem'),
    url(r'^someitems$', ListSomeItemsView.as_view(), name='someitems'),
]
视图.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__'
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'
urlpatterns = [
    url(r'^someitems/add$', SomeItemAddView.as_view(), name='add-someitem'),
    url(r'^someitems$', ListSomeItemsView.as_view(), name='someitems'),
]
url.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__'
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'
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 %}
{%extends“base.html”%}
{%load i18n%}
{%load crispy_forms_tags%}
{%block content%}
{%crispy form%}
{%endblock内容%}

在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”,并调用父form类的init函数,而不传递一些重要的kw参数。因此,一般来说:您只传递额外的关键字参数,而忘记了其他参数-来自表单、ModelForm等

你看过吗?你提到的帖子有相同的前提条件:它使用内联表单。“如果我将helper.field_模板更改为另一个值(或将其删除以设置默认值),则错误将显示在每个字段的上方”->在我的应用程序中并非如此,我不会使用默认设置显示错误。非常感谢@Simeon Popov,现在它可以工作并显示错误。在这之前,它的形式似乎是不受约束的。我不知道父类也期望来自kwargs dict的一些参数。