这是我第一次使用crispy-forms,我正在尝试使用bootstrap 3基于ModelForm创建水平的crispy-form。在模板“ horizo​​ntal-form”的输出中,显示了“ label_class”和“ field_class”。我浏览了许多文档和stackoverflow,包括this question,似乎没有任何效果。

我的表格:

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>


这是我关于堆栈溢出的第一个问题,所以请让我知道如何改进将来的问题或研究方法。

谢谢,
内森

最佳答案

附加到settings.py

CRISPY_TEMPLATE_PACK = 'bootstrap3'

10-04 15:26