django-crispy-forms:form_class出现,但是label_class和field_class没有 [英] django-crispy-forms: form_class appears but label_class and field_class does not
问题描述
这是我第一次使用crispy形式,我试图使用引导程序3创建一个基于ModelForm的水平脆饼。在模板的输出中,horizontal-form显示出来,但是label_class和 field_class。我浏览了许多文档和stackoverflow,包括这个问题,似乎没有任何效果。
This is my first time using crispy-forms and I am trying to make a horizontal crispy-form based on a ModelForm using bootstrap 3. In the output to the template "horizontal-form" shows up but "label_class" and "field_class". I've looked through many the documentation and stackoverflow, including this question and nothing seems to work.
我的表单:
from django.forms import ModelForm
from panews.models import Story
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout
class StoryForm(ModelForm):
class Meta:
model = Story
fields = ['title', 'subtitle', 'content', 'variables']
def __init__(self, *args, **kwargs):
super(StoryForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_method = "POST"
self.helper.form_class = "horizontal-form"
self.helper.label_class = "col-lg-2"
self.helper.field_class = "col-lg-8"
self.helper.layout = Layout(
'title',
'subtitle',
'content',
'variables',
)
这是模板:
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block main %}
<div class="container">
{% crispy form %}
</div>
{% endblock %}
以下是输出:
<main>
<div class="container">
<form class="horizontal-form" method="post" ><input type='hidden' name='csrfmiddlewaretoken' value='phDTwXgeNifQ8DJT8VWtG2stLEDA4LQS' /> <div id="div_id_title" class="control-group"><label for="id_title" class="control-label requiredField">
Title<span class="asteriskField">*</span></label><div class="controls"><input class="textinput textInput" id="id_title" maxlength="50" name="title" type="text" /> </div></div><div id="div_id_subtitle" class="control-group"><label for="id_subtitle" class="control-label ">
Subtitle
</label><div class="controls"><input class="textinput textInput" id="id_subtitle" maxlength="50" name="subtitle" type="text" /> </div></div><div id="div_id_content" class="control-group"><label for="id_content" class="control-label ">
Content
</label><div class="controls"><textarea class="textarea" cols="40" id="id_content" name="content" rows="10"></textarea></div></div><div id="div_id_variables" class="control-group"><label for="id_variables" class="control-label ">
Variables
</label><div class="controls"><textarea class="textarea" cols="40" id="id_variables" name="variables" rows="10"></textarea></div></div></form>
</div>
</main>
这是我的第一个堆栈溢出问题,请让我知道如何改进未来的问题或我的研究方法。
This is my first question on stack overflow so please let me know how I can improve future questions or my research methods.
谢谢,
Nathan
Thanks, Nathan
推荐答案
追加到settings.py
Append to settings.py
CRISPY_TEMPLATE_PACK = 'bootstrap3'
这篇关于django-crispy-forms:form_class出现,但是label_class和field_class没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!