gpt4 book ai didi

jquery - Django 自动完成功能与 django-ajax-selects

转载 作者:太空宇宙 更新时间:2023-11-03 18:25:10 24 4
gpt4 key购买 nike

我需要帮助在我的 Django 项目中实现自动完成字段。当用户输入查询时,我试图从数据库中获取相关项目的列表。

我正在使用django-ajax-selects package

我的项目名称是computer builderbuildsparts是应用程序。我的文件结构如下所示:

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 页面上,当我在测试服务器上时,在搜索字段中输入不会从数据库中显示任何结果: /image/09zDi.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" 

有人可以帮我解决这个问题吗?谢谢!

最佳答案

您还需要将 ajax 查找 View 添加到您的 urls.py

from django.conf.urls.defaults import *

from django.contrib import admin
from ajax_select import urls as ajax_select_urls

admin.autodiscover()

urlpatterns = patterns('',
# include the lookup urls
(r'^autocomplete/', include(ajax_select_urls)),
)

正如你所说:

/admin/lookups/ajax_lookup/moboList, however when I visit that link, I get a 404:

这样您就可以立即指出问题所在。

关于jquery - Django 自动完成功能与 django-ajax-selects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23351326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com