Search code examples
pythondjangotwitter-bootstrap-3django-crispy-forms

add an icon to django crispy form submit button


I am struggling with the Button on a crispy form. Instead of just the default button I would like to have an icon inside

The form I am currently using is like

class MyForm(forms.ModelForm):

helper = FormHelper()
helper.layout = Layout(
    Div(
        Div(PrependedText('source_text', '<span class="fa fa-user"></span>'), css_class='col-md-6'),
        Div(PrependedText('destination_text','<span class="fa fa-flag-checkered"></span>'), css_class='col-md-6'),
        css_class='row-fluid'),
    Div(
        Div(PrependedText('departure', '<span class="fa fa-clock-o"></span>'), css_class='col-md-3'),
        Div('departure_delta', css_class='col-md-2'),
        Div(Submit('submit', "Neue Fahrt starten", css_class="btn"), css_class="col-md2"),
        css_class='row-fluid'),
)
helper.form_show_labels = False
helper.form_id = 'id_travelshare'

class Meta:

    model = MyModel
    fields = ['source', 'source_text', 'destination',
              'destination_text', 'departure', 'departure_delta']

    widgets = {
        'source': forms.HiddenInput(),
        'destination': forms.HiddenInput(),
        'departure' : forms.DateTimeInput(attrs={'class':'datetimepicker'}),
    }

What I want is a button with an icon inside. I did not find an example for this. Maybe somebody has one.

Kind regads

Michael


Solution

  • This worked perfectly for me:

    PrependedText('price', '<span class="fas fa-lira-sign"></span>')
    

    This seemed like the easiest method compared the above solutions which were also worthy :-)