我需要在我的Django项目中实现自动完成字段的帮助。 当用户在查询中键入时,我试图从数据库中获取相关项的列表。在
我正在使用django-ajax-selects package。在
我的项目名是computer builder
,builds
和{
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:
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源代码显示autocomplete是关闭的,源代码是/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"
有人能帮我吗?谢谢!在
您还需要将ajax查找视图添加到网址.py在
正如你所说:
所以你应该马上指出问题所在。在
相关问题 更多 >
编程相关推荐