提交按钮不再适用于django crispy表单

joz*_*yqk 4 django django-templates django-crispy-forms

我已经在我的页面中添加了bootstrap并试图让django crispy表单起作用.真的都是我所做的是pip install django-crispy-forms,加入crispy_formsINSTALLED_APPS和改变{{ form }},以{% crispy form %}我的模板(后加引导和jQuery我的静态目录):

{% load crispy_forms_tags %}

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="." method="post">
{% csrf_token %}
{% crispy form %}
<input type="submit" value="Submit" class="btn btn-success">
</form>
Run Code Online (Sandbox Code Playgroud)

这种形式过去工作得很好.在更改之后,它看起来好多了,但是提交按钮不再做任何事情,因为它被移动到表单之外:

在此输入图像描述

我很惊讶地看到模板更改会影响文档其余部分的布局.我做错了什么或者这是一个错误吗?

Django 1.8.1,django-crispy-forms 1.4.0

Ala*_*air 18

由于您自己在模板中包含表单标记,csrf标记和提交按钮,因此应使用crispy过滤器而不是crispy标记.

<form action="." method="post">
    {% csrf_token %}
    {{ form|crispy }}
    <input type="submit" value="Submit" class="btn btn-success">
</form>
Run Code Online (Sandbox Code Playgroud)

如果要使用标记,则可以在a中定义提交按钮FormHelper.有关详细信息,请参阅crispy tag docs.

  • 在这种情况下,我无法使用表单助手。我怎样才能做到这一点? (2认同)

mpk*_*asp 8

对于其他使用标签卡在此处并希望将一些自定义 html 添加到他们的<form>...

您可以将该self.helper.form_tag = False属性添加到您的助手中,并且它不会</form>在表单末尾附加 a 。

更多文档在这里


Che*_*eng 5

要将“提交”按钮添加到您的表单中,crispy_form的文档是通过以下方式做到的:

import floppyforms.__future__ as forms # you can use django's own ModelForm here
from crispy_forms.helper import FormHelper
from django.core.urlresolvers import reverse_lazy

class YourForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(YourForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_method = 'post' # this line sets your form's method to post
        self.helper.form_action = reverse_lazy('your_post_url') # this line sets the form action
        self.helper.layout = Layout( # the order of the items in this layout is important
            'field1_of_your_model',  # field1 will appear first in HTML
            'field2_of_your_model',  # field2 will appear second in HTML
            # this is how to add the submit button to your form and since it is the last item in this tuple, it will be rendered last in the HTML
            Submit('submit', u'Submit', css_class='btn btn-success'), 
    )

    class Meta:
        model = YourModel
Run Code Online (Sandbox Code Playgroud)

然后,在您的模板中,您要做的就是

{% load crispy_forms_tags %}
{% crispy form %}
Run Code Online (Sandbox Code Playgroud)

就是这样。无需在模板中编写任何html。

我认为crispy_forms的全部目的是在Python中定义HTML。这样您就不必在模板中编写太多HTML。


一些附加说明:

由于您正在使用引导程序。在__init__()上面定义的内部,还有另外三个对您有帮助的字段,如果需要,请添加这些字段:

self.helper.form_class = 'form-horizontal' # if you want to have a horizontally layout form
self.helper.label_class = 'col-md-3' # this css class attribute will be added to all of the labels in your form. For instance, the "Username: " label will have 'col-md-3'
self.helper.field_class = 'col-md-9' # this css class attribute will be added to all of the input fields in your form. For isntance, the input text box for "Username" will have 'col-md-9'
Run Code Online (Sandbox Code Playgroud)