Django Autocomplete Light Empty Select Box

741 views Asked by At

Why does implementation of django-autocomplete-light result in an empty dropdown box? I think it may either be with loading static files or mapping to urls.py incorrectly.

EDIT: do I need to collect the static files used by dal somehow? With collectstatic? I realized yes, so I collected my files and checked the path to them, but nothing is changing.

I have seen similar questions in GitHub issues, but the solutions there didn't affect my situation, and the solutions were generally just compromises with the type of field used.

I have followed the dal tutorial and resulted in the following files. These files are all within my actions app, including the urls.py:

models.py

class Entity(models.Model):
    entity = models.CharField(primary_key=True, max_length=12)
    entityDescription = models.CharField(max_length=200)
    def __str__(self):
        return self.entityDescription

class Action(models.Model):
    entity = models.ForeignKey(Entity, on_delete=models.CASCADE, db_column='entity')
    action = models.CharField(max_length=50)
    def __str__(self):
        return '%s' % self.entity

forms.py:

class ActionForm(ModelForm):
class Meta:
    model = Action
    fields = '__all__'
    widgets = {
        'entityDescription': autocomplete.ModelSelect2(url='eda')
    }

ActionFormSet = modelformset_factory(Action, extra=1, exclude=(), form=ActionForm)

I am rendering the formset with crispy-forms.

views.py:

class EntityAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
    qs = Entity.objects.all()
    if self.q:
        qs = qs.filter(entityDescription__istartswith=self.q)
    return qs

urls.py:

urlpatterns = [
    url(r'actions', views.actions, name='actions'),
    url(
        r'^eda/$',
        views.EntityDescriptionAutocomplete.as_view(),
        name='eda',
    ),
]

This is in my template:

{% block footer %}
<script type="text/javascript" src="/static/collected/admin/js/vendor/jquery/jquery.js"></script>
{{ form.media }}
{% endblock %}

Going to http://127.0.0.1:8000/eda results in seemingly proper JSON:

{"results": [{"id": "123", "text": "123 - Michael Scott"}, {"id": "234", "text": "234 - Jim Halpert"}, {"id": "345", "text": "345 - Pam Beasley"}], "pagination": {"more": false}}

Reversing the url...

>>>from django.urls import reverse
>>>reverse('eda')
'/eda/'

...seems to work as expected

With this setup, the result is a select box with Django's ------- signifier:

enter image description here

If I removed the widget setting in the meta class and instead override the field directly:

class ActionForm(ModelForm):
    entityDescription = ModelChoiceField(
        queryset=Entity.objects.all(),
        widget=autocomplete.ModelSelect2(url='eda')
        )
    class Meta:
        model = Action
        fields = '__all__'

...the result is the same.

Do I need to do something else to get the correct static files? Am I mapping to urls.py incorrectly?

0

There are 0 answers