我在django项目中使用django-crispy-forms,并在documentation中阅读,我看到要能够使用bootstrap3功能(如horizontal forms),我需要通过添加以下内容将bootstrap3设置为我的香脆模板包我的项目的settings.py中的行:

CRISPY_TEMPLATE_PACK = 'bootstrap3'


根据文档,crispy的默认值为bootstrap v2。但是在设置中添加bootstrap3之后,当我在开发计算机上运行应用程序时,出现以下错误:

TemplateDoesNotExist at /dashboard/
bootstrap3/field.html
Request Method: POST
Request URL:    http://localhost:8000/dashboard/
Django Version: 1.7.3
Exception Type: TemplateDoesNotExist
Exception Value:
bootstrap3/field.html
Exception Location: C:\Python27\VirtualEnvs\Tlaloc\lib\site-packages\django\template\loader.py in find_template, line 136
Python Executable:  C:\Python27\VirtualEnvs\Tlaloc\Scripts\python.exe
Python Version: 2.7.7


如果我从设置中删除了CRISPY_TEMPLATE_PACK行(使用默认设置)或将其更改为如下所示:

CRISPY_TEMPLATE_PACK = 'bootstrap'


然后,我再也不会收到错误,但是表单水平类在我的表单中不起作用。

这就是forms.py中我的表单的样子

class UserForm(forms.Form):
    user = forms.CharField(label='Account', max_length=15)
    password = forms.CharField(widget=forms.PasswordInput())

    # Crispy forms code
    def __init__(self, *args, **kwargs):
        super(UserForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-sm-2'
        self.helper.field_class = 'col-sm-10'
        self.helper.layout = Layout(
            Fieldset(
                '',
                'user',
                'password',
            ),
            Div(FormActions(
                Submit('continue', 'Continue', css_class='btn btn-primary'),
                Button('cancel', 'Cancel', css_class='btn btn-default',
                       data_dismiss='modal'),
                ),
                css_class='modal-footer'
                )
            )


这是我的模板的一部分:

{% load crispy_forms_tags %}

<div class="modal fade" id="adAccountModal" tabindex="-1" role="dialog" aria-labelledby="authenticationLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">

                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="authenticationLabel">{{ config_values.environment_name }} Environment Authentication</h4>
                    </div>

                    <div class="modal-body">
                        <p>Please enter the account and password that will be used to authenticate in the selected environment.</p>
                        {% crispy user_form %}
                    </div>

                    {% comment %}
                    The footer will be added through the user_form using Crispy Forms.
                    The following code will be just left here as reference.
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                        <button type="button" class="btn btn-primary">Continue</button>
                    </div>
                    {% endcomment %}
                </div>
            </div>
        </div>


我究竟做错了什么?

最佳答案

原来,我的脆性表格安装中不存在bootstrap3模板目录。

我的Windows系统上安装了Crispy Forms 1.3.2版本。在github的项目页面中,我看到当前版本为1.4.0,确实有\ crispy_forms \ templates \ bootstrap3目录。看起来bootstrap3模板包是在此版本之前引入的,较早的版本没有模板包。我已升级到当前版本,现在可以使用了。

07-26 02:49