0

I am using django-easy-select2 to handle the entering of data into several manytomanyfields within a model - titled Engagement.

I am using bootstrap and crispy forms to render the Engagement form to HTML.

The rendering is broadly working as expected/required. However, the size of form fields for manytomany data are initially very small and require data to the selected/entered, before they expand. Once data is entered the fields do expand. But, I would like these fields to initially render as the size set by bootstrap.

For example, I've set bootstrap as col-6, but the initial render of the manytomany is only col-1 or even less. When data is entered that field will expand up to col-6 and no further, which good, but I would like the field to start at col-6, even with no data.

Relevant code is below.

engagements.view:

class EngagementCreateView(CreateView):
    model = Engagement
    form_class = select2_modelform(Engagement, attrs={'width': 'auto'})  # this sets the widths of the field
    template_name = "engagements/engagement_create.html"

    def form_valid(self, form):
        print(form.cleaned_data)
        return super().form_valid(form)

create engagement template

{% extends 'base.html' %}

{% load crispy_forms_tags %}

{% block content %}

    <h4>{% if form.instance.pk %}Edit engagement{% else %}Create new engagement{% endif%}</h4>

    <div class="form-group">
        <form method="post" novalidate>
            {% csrf_token %}
            <div class="row mt-1">
                <div class="col-6">   # I'm rendering the width of fields here
                    {{ form.date|as_crispy_field }}
                    {{ form.projects|as_crispy_field }}   
                    {{ form.stakeholders|as_crispy_field }}
                    {{ form.ppdds|as_crispy_field }}
                </div>
                <div class="col-6">
                    {{ form.follow_up_date|as_crispy_field }}
                    {{ form.engagement_types|as_crispy_field }}
                    {{ form.engagement_workstreams|as_crispy_field }}
                </div>
            </div>
            <div class="row mt-1">
                <div class="col-lg">
                    {{ form.summary|as_crispy_field }}
                </div>
            </div>

            <br>
            <input class="btn btn-primary" type="submit"
                   value="{% if form.instance.pk %}Save{% else %}Create{%    endif%}">
                    <a class="btn btn-primary" href="../">Cancel</a></p>

        </form>
    </div>
{% endblock %}

I think the most relevant part of the django-easy_select2 documentation is here https://django-easy-select2.readthedocs.io/en/latest/usage.html.

BiswajitPaloi
  • 586
  • 4
  • 16
Banillie
  • 49
  • 9
  • 1
    Check this post: https://stackoverflow.com/questions/12683907/set-the-width-of-select2-input-through-angular-ui-directive – Marco Jan 07 '22 at 10:06
  • @Marco . Thank you so much. As per top answer in the link you posted, changing attrs={'width': 'auto'} to attrs={'width': '100%'} works perfectly. I'm very grateful for your help. – Banillie Jan 09 '22 at 19:02

0 Answers0