我需要在我的Django項目中實現自動完成的領域的幫助。 我正試圖從用戶在查詢中鍵入數據庫中獲取相關項目的列表。我正在使用django-ajax-selects package。Django的自動完成功能與Django的AJAX選擇
我的項目名稱是computer builder
和builds
和parts
是應用程序。 我的文件結構如下所示:
parts/models.py:
from django.db import models
# Create your models here.
class moboListing(models.Model):
"""This model makes a database for a list of motherboards and prices"""
id = models.AutoField(primary_key=True)
moboList = models.CharField(max_length=400)
moboPrice = models.DecimalField(max_digits=5, decimal_places=2)
在數據庫中,moboListing填充了主板的名稱。
builds/models.py:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class BuildsTable(models.Model):
moboPart = models.CharField(max_length=400)
moboPrice = models.DecimalField(max_digits=5, decimal_places=2)
builds/forms.py:
from django.forms.models import ModelForm
from django import forms
from ajax_select import make_ajax_field
from parts.models import moboListing
from builds.models import BuildsTable
class BuildsForm(ModelForm):
class Meta:
model = moboListing
moboList = make_ajax_field(moboListing, 'moboList', 'moboList', help_text=None
builds/lookups.py:
from ajax_select import LookupChannel
from django.db.models import Q
from django.utils.html import escape
from parts.models import moboListing
class moboLookup(LookupChannel):
model = moboListing
def get_query(self, q, request):
return moboListing.objects.filter(Q(moboList__icontains=q)).order_by('name')
def get_result(self, obj):
""" result is the simple text that is the completion of what the person typed """
return obj.name
def format_match(self, obj):
""" (HTML) formatted item for display in the dropdown """
return self.format_item_display(obj)
def format_item_display(self, obj):
""" (HTML) formatted item for displaying item in the selected deck area """
return u"%s<div><i>%s</i></div>" % (escape(obj.moboList), escape(obj.moboPrice))
def get_objects(self, ids):
return moboListing.objects.filter(pk__in=ids)
builds/views.py
:
from django.shortcuts import render, render_to_response
from django import forms
import datetime
from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from ajax_select.fields import AutoCompleteField
from forms import BuildsForm
from builds.models import BuildsTable
from parts.models import moboListing
# Create your views here.
class SearchForm(forms.Form):
q = AutoCompleteField(
'moboList',
required=True,
help_text="Autocomplete will suggest motherboards",
label="Motherboards",
attrs={'size': 400}
)
@login_required
def new_build(request):
dd = {}
if 'q' in request.GET:
dd['entered'] = request.GET.get('q')
initial = {'q': "Enter Motherboard query"}
form = SearchForm(initial=initial)
dd['form'] = form
return render_to_response('new_build.html', dd, context_instance=RequestContext(request))
在settings.py
:
# define the lookup channels in use on the site
AJAX_LOOKUP_CHANNELS = {
#simple: search Person.objects.filter(name__icontains=q)
#'person' : {'model': 'example.person', 'search_field': 'name'},
# define a custom lookup channel
#'song' : ('example.lookups', 'SongLookup')
#'moboList' : {'model': 'parts.moboListing', 'search_field': 'moboList'},
'moboList': ('builds.lookups', 'moboLookup')
}
AJAX_SELECT_BOOTSTRAP = True
然而,在我的html頁面,當我在測試服務器上,輸入到搜索字段並不帶來了從數據庫中的任何結果: http://i.imgur.com/3o1PONH.jpg
另外,我的html源代碼顯示自動完成功能已關閉,並且源爲/admin/lookups/ajax_lookup/moboList
,但是當我訪問該鏈接時,我得到一個404:
<tr><th><label for="id_q">Motherboards:</label></th><td><input type="text" name="q" id="id_q" value="Enter Motherboard query" autocomplete="off" data-ajax-select="autocomplete" data-plugin-options="{min_length: 1, initial: Enter Motherboard query, html: true, source: /admin/lookups/ajax_lookup/moboList}" maxlength="255"
任何人都可以幫助我嗎?謝謝!
謝謝!我如上所述添加了網址,而不是如何在文檔中,現在我沒有收到404頁面,我終於可以在管理面板中進行自動完成搜索。當我在管理面板中進行搜索時,我可以從終端看到輸入查詢時對服務器所做的請求,但是,當我在網站的前端嘗試相同時,似乎沒有腳本正在運行主動搜索,並且我的終端不顯示向服務器發出的請求。我需要做什麼特別的事情來讓腳本運行嗎? – achabacha322
謝謝,我得到它的工作,我錯過了bootstrap。js在頁面 – achabacha322
文檔中的示例假設您使用的是管理員,因此它將url模式放在/ admin /之前,以便ajax網址匹配。也許你把它放在/ admin/include之後,這樣就可以吞併所有請求到/ admin/ 我可能會更改文檔示例 – felix