擅长:python、mysql、java
<p>如果您查看<a href="https://jqueryui.com/autocomplete/" rel="nofollow noreferrer">this jQuery autocomplete example</a>的源代码,就会发现<code>source</code>参数实际上使用的是一个列表</p>
<p>因此,视图应该返回字符串的JSON列表(目前,它只返回一个字符串)。这可以通过以下方式实现:</p>
<pre><code>from django.http import JsonResponse
def autocomplete(request):
if request.method == 'GET':
cities = City.objects.filter(title__istartswith=request.GET.get('term', None)).values_list('title', flat=True)
if cities:
return JsonResponse(cities)
else:
return HttpResponse('Не правильная раскладка', content_type="text/plain")
</code></pre>
<p>关于源参数的更多信息可以在这里找到:<a href="http://api.jqueryui.com/autocomplete/#option-source" rel="nofollow noreferrer">http://api.jqueryui.com/autocomplete/#option-source</a></p>