django crispy-forms & floppyforms w/ bootstrap: how to get help_text into a ModelForm?

django-floppyforms, django-forms, twitter-bootstrap

Solution

I found out what the problem was. Since I didn't really define the Fields anywhere (I only defined the field widgets), I couldn't access the help_text. You can either define the Fields in forms.py (which is unnecessary and bad practice, they're ModelFields after all) or set the help_text for the model in models.py.

Hope that helps others who have a similar problem.

Problem

I'm having a small (but annoying) problem with django forms right now. I'm using: - twitter bootstrap - django floppyforms (just for the html5 widgets) - django crispy-forms (for the template tags & rendering) - my forms are all ModelForms and that shouldn't change if possible I've searched the whole web and tried a lot of stuff, but I can't figure out the place where I can inject help_text="Some random help text" into the code. So here's my code(abbreviated for sanity reasons): ``` #forms.py: import floppyforms as forms from crispy_forms.helper import FormHelper from crispy_forms.layout import * from crispy_forms.bootstrap import * from courses.models import * class CourseForm(forms.ModelForm): class Meta: model = Course widgets = { 'title': forms.TextInput, # This is a floppyforms widget ... } def __init__(self, *args, **kwargs): self.helper = FormHelper() self.helper.form_method = 'POST' self.helper.form_id = '' self.helper.form_class = 'form-horizontal' self.helper.form_action = '' # redirect in the view self.helper.form_tag = True self.helper.help_text_inline = True # means that I want <span> elements self.helper.layout = Layout( Fieldset('Create a new course', # fieldset label Field('title', placeholder="Something...", css_class="span4"), ... ), FormActions( Submit( 'submit', 'Submit', css_class="btn-primary" ) ), ) super(CourseForm, self).__init__(*args, **kwargs) ``` I tried to inject it as an 'attrs' dict into the widget and as an attr into the Field. ``` forms.TextInput(attrs={'help_text': 'Some help text'}) Field('title', help_text="Some help text", css_class="span4") ``` Needless to say that it didn't work. I need a hook to put the help text into a 'span' or 'p' inside my controls-div, not into the input widget. My template is very minimal and it should stay that way if possible. I don't want to iterate over the form fields: ``` #create_course.html {% extends 'base.html'%} {% load crispy_forms_tags %} {% block content%} {% crispy form %} {% endblock content%} ``` That renders as the following html: ``` <div id="div_id_title" class="clearfix control-group"> <label class="control-label requiredField" for="id_title"> Title <span class="asteriskField">*</span> </label> <div class="controls"> <input id="id_title" class="span4 textinput textInput" type="text" placeholder="Something..." maxlength="60" required="" name="title"> </div> </div> ``` With the help text it should look like this: ``` <div id="div_id_title" class="clearfix control-group"> <label class="control-label requiredField" for="id_title"> Title <span class="asteriskField">*</span> </label> <div class="controls"> <input id="id_title" class="span4 textinput textInput" type="text" placeholder="Something..." maxlength="60" required="" name="title"> **<span class="help-inline">Supporting help text</span>** </div> </div> ``` Any help is appreciated! I haven't tried to inject the code through views.py into the form, but I see no point in doing that. It should be possible to do that in forms.py with the right hook and syntax. Sorry for the long text for such a simple problem ;)

Original source