2017-02-18 44 views
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') 
     } 

回答

1
from pip import autocomplete 

這是如何關係到你想添加的功能? Pip是爲你安裝python軟件包的程序,這只是一個實用功能。

那麼你是由於某種原因試圖從這個函數得到一個ModelSelect2對象?

我認爲你剛把你的進口搞砸了,你想用的是django_select2.forms.ModelSelect2Widget(假設你使用的是django-select2)。