我正在尝试将autocomplete-light安装到我的一个表单中。我一直收到这个错误:
Institution not registered, you have registered: ['InstitutionAutocomplete']
我没有在我的代码中的任何地方声明或注册InstitutionAutocopmlete,因此我不知道如何触发此错误。
我有以下表格:
class AddUserAccountsForm(forms.ModelForm):
"""
Form for adding new user financial accounts.
"""
required_css_class = 'required'
name = forms.ModelChoiceField(required=True, queryset=Institution.objects.all(), widget=autocomplete_light.ChoiceWidget('Institution'))
user_accounts_name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'User Name'}), label="")
user_accounts_password = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Password'}), label="")
class Meta:
model = Institution
fields = ('name','user_accounts_name', 'user_accounts_password',)
这是在我的autocomplete_light_registry.py中:
导入autocomplete_light
from models import Institution
autocomplete_light.register(Institution,
search_fields = ['name'],
attrs={
'placeholder':'Find your institution',
'data-autocomplete-minimum-characters':1,
},
widget_attrs={
'data-widget-maximum-values':4,
'class':'modern-style',
},
)
这是我的HTML。代码运行时会触发错误{{form.as_p}}。
{% load staticfiles %}
<script type="text/javascript" src="{% static 'jquery.js' %}">
</script>
{% include 'autocomplete_light/static.html' %}
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
urls.py是:
url(r'^profile/add_user_accounts/', UserAccountsUpdate.as_view(), name='add_user_accounts'),
答案 0 :(得分:1)
在您的表单中,您必须使用“InstitutionAutocomplete”而不是“Institution”
name = forms.ModelChoiceField(required=True,
queryset=Institution.objects.all(),
widget=autocomplete_light.ChoiceWidget('InstitutionAutocomplete'))