Prometheus
Prometheus

Reputation: 33625

Django forms how to add form field attribute

I'm using django-crispy-forms I want to add an attribute http-prefix to the outputted domain field, for example like this...

<input type="text" name="domain" http-prefix>

How is this possible? I can see crispy-forms has the ability to add css to a field self.helper.field_class, but I cannot see where to add an attribute to a field like my example above just http-prefix.

My Form:

class SchemeForm(NgModelFormMixin, forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(SchemeForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-lg-3'
        self.helper.field_class = 'col-lg-8'
        self.helper.layout = Layout(
        'name',
        'domain',
        'slug',


    class Meta:
        model = Scheme
        fields = ('name', 'domain', 'slug')

Upvotes: 0

Views: 387

Answers (1)

mariodev
mariodev

Reputation: 15519

Simply update the attribute by setting the value to empty string:

def __init__(self, *args, **kwargs):
    super(SchemeForm, self).__init__(*args, **kwargs)

    #...
    self.fields['domain'].widget.attrs['http-prefix'] = ''

Upvotes: 3

Related Questions