【发布时间】:2015-03-20 10:45:51
【问题描述】:
我正在为我的 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'
然后我不再收到错误消息,但是 form-horizontal 类在我的表单中不起作用。
这就是我的表单在 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">×</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>
我做错了什么?
【问题讨论】:
-
你有没有尝试在 settings.py 中的模板路径中放入脆皮表单模板文件夹的路径?
-
@dan-klasson 你的意思是这样的吗? TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates'), 'Whatever_the_crispy_form_template_is'] 如果是这样...你知道那个模板文件夹在哪里吗?
-
我会放一条直接路径。该位置取决于您的操作系统。但你可以尝试搜索
django-crispy-forms/crispy_forms/templates。
标签: django django-forms django-crispy-forms