Django визуализирует поле формы с помощью css, используя четкие формы

У меня есть форма, которую я хочу сделать хрустящей,

ФОРМА

class MyForm(forms.ModelForm):

    class Meta:
        model = MyModel
        excludes = ('user', 'created')

    def __init__(self, *args, **kwargs):


        self.helper = FormHelper()
        self.helper.form_show_labels = False ## I only need the input field
        self.helper.layout = layout.Layout(

            layout.Field('name', css_class="form-control"), 
            layout.Field('objective', css_class="form-control"),
            ## ADD CLASS form-control TO THE FIELDS (This is the problem since the fields are not rendered with the class)
        )
        super(MyForm, self).__init__(*args, **kwargs)

ШАБЛОН

<div class="form-group">
    <label>Name <i class="fa fa-question-circle tooltips" data-original-title="Lorem Ipsum" data-html="true" data-placement="right" data-container="body"></i></label>
    {{ form.name|as_crispy_field }}
    {{ form.name.errors|striptags }}
</div>
<div class="form-group">
    <label>Theme/ Objective <i class="fa fa-question-circle tooltips" data-original-title="Lorem Ipsum" data-html="true" data-placement="right" data-container="body"></i></label>
    {{ form.objective|as_crispy_field }}
    {{ form.objective.errors|striptags }}
</div>

Поля не отображаются с классом form-control. Метки отображаются, и они мне не нужны, так как у меня есть свои собственные метки. Любая помощь в том, как я могу отображать поля с классом, будет высоко оценена. Спасибо.


person Alexxio    schedule 13.02.2015    source источник
comment
Можете ли вы пропустить crispy-forms и отобразить форму вручную docs.djangoproject .com/en/1.7/topics/forms/   -  person Selcuk    schedule 13.02.2015
comment
@Сельчук. Спасибо за предложение, как мне добавить класс form-control в поля?   -  person Alexxio    schedule 13.02.2015
comment
Большое спасибо. Кажется, это решает мою проблему.   -  person Alexxio    schedule 13.02.2015


Ответы (1)


Если вы визуализируете свои поля вручную, вы можете добавить их, например:

<input type="text" name="{{ form.objective.html_name }}" 
       id={{ form.objective.id_for_label }}" 
       class="form-control">

Обратите внимание, что это потребует от вас ручного рендеринга всех ваших input.

person Selcuk    schedule 13.02.2015