djangodjango-formsdjango-crispy-forms

Getting TemplateDoesNotExist error when using bootstrap3 as template pack for crispy forms


I am using django-crispy-forms for my django project, and reading in the documentation I saw that to be able to use bootstrap3 features (like horizontal forms), I need to set bootstrap3 as my crispy template pack by adding this line in the settings.py of my project:

CRISPY_TEMPLATE_PACK = 'bootstrap3'

According to the docs, crispy's default is bootstrap v2. But after adding bootstrap3 in my settings, when I run my application in my development machine I get this error:

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

If I remove the CRISPY_TEMPLATE_PACK line from my settings (as to use the defaults) or change it to look like this:

CRISPY_TEMPLATE_PACK = 'bootstrap'

Then I don't get an error anymore, but then the form-horizontal class doesn't work in my form.

This is how my form looks like in 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'
                )
            )

And this is part of my template:

{% 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>

What am I doing wrong?


Solution

  • Turned out that the bootstrap3 template directory didn't exist in my installation of crispy forms.

    I had Crispy Forms version 1.3.2 installed on my Windows system. And looking in the project page in github I saw that the current version, which at the moment is 1.4.0, did have the \crispy_forms\templates\bootstrap3 directory. Looks like bootstrap3 template pack was introduced until this version, older versions didn't have the template pack. I upgraded to the current version, and it is now working.