is posible to render wtf.form_field with out label? is posible to render wtf.form_field with out label? flask flask

is posible to render wtf.form_field with out label?


Coming to this a bit late but I find the easiest way is to use CSS to set the visibility of the label to None.

.control-label {display: none;

}

The above CSS will make all labels invisible. This may not be desirable for every label but if add a "no-label" class to specific WTF form fields (or indeed a higher element that groups the fields) the following CSS will not display the labels for those fields:

.no-label .control-label {display: none;

}


You're using a bootstrap-themed macro to render the wtform object, there's no reason you can't create your own customized macro based off that existing one, that does exactly the same thing except without the .label rendering.

For instance, the code for the macro you're using is located on github. I could copy all of that macro and put it in a new custom_wtf.html template file, and rename the macro to be 'wtf_nolabel` and work on adjusting it to my needs.

Lets take lines 83-93 for an example, this seems to render all inline form elements that aren't already handled above:

{%- if form_type == "inline" %}    {{field.label(class="sr-only")|safe}}    {% if field.type == 'FileField' %}      {{field(**kwargs)|safe}}{% else %}

I could just remove the {{ field.label(class="sr-only")|safe }} line and that would now work for the inline elements, moving down below the code noted, I'd remove line 97-99 to adjust the horizontal rendering option as it's currently:

{{field.label(class="control-label " + (  " col-%s-%s" % horizontal_columns[0:2]))|safe}}

If your form element is just one particular type of input, you could build your own reduced macro that just targets that form element.


If you're not minded to edit the template macros as outlined by @Doobeh and you don't want to show any field labels you could use the following technique.

Create a "No Label" mixin class that sets the label property of all form fields to None.

class NoLabelMixin(object):    def __init__(self, *args, **kwargs):        super(NoLabelMixin, self).__init__(*args, **kwargs)        for field_name in self._fields:            field_property = getattr(self, field_name)            field_property.label = Noneclass MyForm(Form):    first_name = StringField(u'First Name', validators=[validators.input_required()])    last_name  = StringField(u'Last Name', validators=[validators.optional()])class MyNoLabelForm(NoLabelMixin, MyForm):    passmy_no_label_form = MyNoLabelForm()