question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to set class of select2 form

See original GitHub issue

DAL is working great in my project, thanks for making such an amazing app. Problem is it’s not looking great and I can’t figure out how to set the form class properly.

I would like to set the form class to bootstrap ‘form-control input-lg’

Changing the class in the template just renders a the DAL form within the bootstrap form, even if I drop the form group and just use a single form tag.

Cannot figure out how to set in the forms.py with the data attributes, setting class = ‘class name’ here has not effect.

Forms.py

class CreatePartForm(forms.ModelForm):
    part = forms.ModelChoiceField(
        queryset=Design.objects.all(),
        widget=autocomplete.ModelSelect2(url='PartsAutocomplete',attrs={
            'data-placeholder': 'Add a part to your BOM',
            })
    )
    class Meta:
        model = Design
        fields = ('part',)

    def __init__(self, *args, **kwargs):
        super(CreatePartForm, self).__init__(*args, **kwargs)
        self.fields['part'].label = ''

Template.html


    <form class='form'  method='POST' action='#'>
        <div class='form-group'>
            {% csrf_token %}
            {{ PartForm|crispy }}
        </div>
        <button type='submit' class='btn btn-success btn-lg' value='part'>
            Add Part
        </button>
    </form>

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

15reactions
ikcamcommented, Jun 15, 2016

I don’t know if this could help you but I overwrite some of the CSS classes to make it look better with bootstrap.

/* dal bootstrap css fix */
.select2-container {
    width: 100% !important;
    min-width: 10em !important;
}
/* django-addanother bootstrap css fix */
.related-widget-wrapper{
    padding-right: 16px;
    position: relative;
}
.related-widget-wrapper-link{
    position: absolute;
    top: 3px;
    right: 0px;
}`
1reaction
josyladcommented, May 25, 2020

I don’t know if this could help you but I overwrite some of the CSS classes to make it look better with bootstrap.

/* dal bootstrap css fix */
.select2-container {
    width: 100% !important;
    min-width: 10em !important;
}
/* django-addanother bootstrap css fix */
.related-widget-wrapper{
    padding-right: 16px;
    position: relative;
}
.related-widget-wrapper-link{
    position: absolute;
    top: 3px;
    right: 0px;
}`

This helped, but I also added some custom Styling to make the fields look good and responsive.

.select2-container--default .select2-selection--single .select2-selection__rendered {
  line-height: 100% !important;
}

.select2-container .select2-selection--single {
  height: 40px !important;
}

.select2-container .select2-selection--multiple {
  
  min-height: 45px !important;
}

.select2-container--default .select2-selection--multiple .select2-selection__rendered {
  
  height: 100% !important;
}
.select2-container .select2-selection--multiple .select2-selection__rendered {
  overflow: auto !important;
  
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Add class to select2 element - jquery - Stack Overflow
I'm trying to add an error class to a select2 box for form validation. It's just a 1px red border. I found the...
Read more >
Appearance | Select2 - The jQuery replacement for select boxes
Select2 is a jQuery based replacement for select boxes. It supports searching, remote data sets, and pagination (infinite scrolling) of results.
Read more >
Need an additional custom class to select2-container #285
I am using version 4.0.2 . And on using containerCssClass it adds class to select2-selection . I guess the class is supposed to...
Read more >
select2-bootstrap-theme
Those of you familiar with Select2 v3 remember that the default behavior was to copy the original elements CSS-classes to the container of...
Read more >
Select2 and Bootstrap Integration by Keenthemes
Select2's CSS is highly customized in sass/vendors/plugins/_select2. · Select2's Javascript is globally initialized with some predefined settings in src/js/ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found