Thomas
Thomas

Reputation: 2276

Django Autocomplete Light in admin area is not getting result

I am facing some issue with Django Autocomplete Light in admin area, I doubled checked all configuration and code but I am not sure where is the problem to solve it.

It seems that in the Admin page is not getting the Autocomplete Integration. It looks like:

enter image description here

Here are part of the code that was developed:

views.py

from dal import autocomplete
from ocup.models import Ocup


class OcupAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        if not self.request.user.is_authenticated():
            return Ocup.objects.none()

        qs = Ocup.objects.all()

        if self.q:
            qs = qs.filter(titulo__istartswith=self.q)

        return qs

(Ocup.objects.all() brings a lot of objects when used in the terminal, the database is populated)

forms.py:

    from ocup.models import Ocup
    from dal import autocomplete
    from django import forms


class OcupForm(forms.ModelForm):

    ocup = forms.ModelChoiceField(
        queryset=Ocup.objects.all(),
        widget=autocomplete.ModelSelect2(url='ocup-autocomplete')
    class Meta:
        model = Ocup
        fields = ('__all__')

admin.py

class OporAdmin(admin.ModelAdmin):
    form = OcupForm
(.......)

model.py

 class Ocup(models.Model):
    familia = models.ForeignKey(Familia)
    codigo = models.CharField(max_length=10)
    titulo = models.CharField(max_length=200)

    def __str__(self):
        return self.titulo.encode('utf-8')

settings.py

INSTALLED_APPS = (
'dal',
'dal_select2',
'django.contrib.admin',
(.....)

The javascript files are being loading: enter image description here

I think that I added all pertinent information, with something is missing just let me know.

--- UPDATE --- I noticed that the Console was giving the following error: enter image description here

So select2 isn't being loading.... Checking the network, I saw that jquery is being loaded twice, but it is the root cause of this problem. But, why it's included twice, and how it's possible to make the Admin area just load once?

enter image description here

Upvotes: 0

Views: 902

Answers (1)

Thomas
Thomas

Reputation: 2276

The solution was to run python manage.py collectstatic

Upvotes: 1

Related Questions