0
我一直在努力使ajax自動完成。我安裝了包自動完成燈並嘗試導入select2但失敗。它會顯示像這樣的錯誤消息。Django:'function'對象沒有屬性'ModelSelect2'
錯誤消息。
'function' object has no attribute 'ModelSelect2'
settings.py
INSTALLED_APPS = [
'dal',
'dal_select2',
'dal_queryset_sequence',
template.html
<form action="{% url 'blog:ajax_article_autocomplete' %}" method="post">
<div class="ui-widget">
<label for="id_articles">search: </label>
<input id="id_articles" type="text" size="50" name="articles" class="ui-autocomplete-input" autocomplete="off">
</div>
</form>
views.py
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.views.generic.edit import FormView
from pip import autocomplete
def ajax_article_autocomplete(request):
if request.is_ajax():
q = request.GET.get('term', '')
articles = Article.objects.filter(title__icontains = q)[:10]
results = []
for article in articles:
article_json = {}
article_json['id'] = article.id
article_json['label'] = article.title
article_json['value'] = article.title
results.append(article_json)
data = json.dumps(results)
else:
data = 'fail'
mimetype = 'application/json'
return HttpResponse(data, mimetype)
urls.py
url(r'^ajax/article/autocomplete/$', views.ajax_article_autocomplete, name='ajax_article_autocomplete'),
forms.py
class SearchForm(forms.ModelForm):
class Meta:
model = User
fields = ('__all__')
widgets = {
'title': autocomplete.ModelSelect2(url='ajax_tag_autocomplete')
}