django-crispy-forms: form_class appears but label_

2019-07-15 08:20发布

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.

My Form:

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',
        )

Here's the template:

{% extends "base.html" %}
{% load crispy_forms_tags %}

{% block main %}

            <div class="container">
                        {% crispy form %}
            </div>

{% endblock %}

Here's the output:

<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.

Thanks, Nathan

1条回答
ゆ 、 Hurt°
2楼-- · 2019-07-15 08:46

Append to settings.py

CRISPY_TEMPLATE_PACK = 'bootstrap3'
查看更多
登录 后发表回答