Django-Auto-Complete Autocomplete not Registered error

1.4k Views Asked by At

when I launch the runserver command on Django, I receive an error which looks like this:

autocomplete_light.exceptions.AutocompleteNotRegistered: CityAutocomplete not registered (registry is empty)

What puzzles me is that CityAutocomplete is registered and I checked this by going to localhost:8000/autocomplete/CityAutocomplete

Does anyone know what's wrong? Here is my forms.py:

class OppForm(forms.ModelForm):
    opp_city = forms.ModelChoiceField(City.objects.all(), widget=autocomplete_light.ChoiceWidget('CityAutocomplete'))

class Meta:
    model = Opportunity
    fields = ('name','start', 'end', 'expiration','description','opp_type','location','street_address','opp_city', 'hours_per_week','opp_pic','extra_field_number','label1','label2','label3','label4','label5',)
    exclude = ('by_booth','opp_by')
def __init__(self, *args, **kwargs):
    super(OppForm, self).__init__(*args, **kwargs)
    self.fields['extra_field_number'].label = "Number of Extra Fields"

Here's my autocomplete_light_registry.py:

import autocomplete_light

from cities_light.models import City

class CityAutocomplete(autocomplete_light.AutocompleteModelBase):
    search_fields = ['^name', ]

autocomplete_light.register(City, CityAutocomplete)

My ultimate goal is to try to use django-cities-light and django-autocomplete to create a form field that allows users to type in a city to get autocomplete suggestions. Thanks again!

Update: One possible problem I thought of is that it's possible that the widget cannot locate CityAutocomplete because of my project structure. My apps are all in a folder called apps in the project directory, instead of being located directly in the root of the project directory.

1

There are 1 best solutions below

1
On

Did you also put autocomplete_light.autodiscover() in forms.py?

The documentation says:

In urls.py, call autocomplete_light.autodiscover() before admin.autodiscover() and before any import of a form with autocompletes, it can look like this...