django-crispy-forms: form_class appears but label_class and field_class does not

☆樱花仙子☆ 提交于 2019-12-24 04:09:21

问题


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:


Append to settings.py

CRISPY_TEMPLATE_PACK = 'bootstrap3'


来源:https://stackoverflow.com/questions/26849485/django-crispy-forms-form-class-appears-but-label-class-and-field-class-does-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!