1
我正在關注如何設置Django-autocomplete字段的教程,並努力讓它工作。這裏的教程:https://django-autocomplete-light.readthedocs.io/en/master/tutorial.htmlDjango - Django的自動完成燈光設置如何
設置已安裝的應用
INSTALLED_APPS = (
'dal',
'dal_select2',
'django.contrib.admin',
項目urls.py
from textchange.views import TextbookAutoComplete
urlpatterns = [
url(r'^textbook-autocomplete$', TextbookAutoComplete.as_view(), name='textbook-autocomplete'),
HTML
<form method="POST"> {% csrf_token %}
{% for field in form3 %}
{{ field }}
{% endfor %}
<input id="search" class="button" type="submit" value="Search Textbooks" name="Search"></input>
</form>
Forms.py
class Search(forms.ModelForm):
longschool = forms.ModelChoiceField(
queryset=Textbook.objects.all(),
widget=autocomplete.ModelSelect2(url='textbook-autocomplete')
)
class_name = forms.ModelChoiceField(
queryset=Textbook.objects.all(),
widget=autocomplete.ModelSelect2(url='textbook-autocomplete')
)
isbn = forms.ModelChoiceField(
queryset=Textbook.objects.all(),
widget=autocomplete.ModelSelect2(url='textbook-autocomplete')
)
class Meta:
model = Textbook
fields = ('longschool', 'class_name', 'isbn')
Views.py
class TextbookAutoComplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
# Don't forget to filter out results depending on the visitor !
if not self.request.user.is_authenticated():
return Textbook.objects.none()
qs = Textbook.objects.all()
if self.q:
qs = qs.filter(name__istartswith=self.q)
return qs
jQuery的加入
<script type="text/javascript" src="{% static "admin/js/jquery.js" %}"></script>
當形式,我的HTML顯示了它是沒有輸入字段只有三個下拉菜單(如,而不會有任何輸入)。任何人都可以看到我缺少的東西嗎?
任何幫助將不勝感激。